APR Hello World

Posted on August 17, 2005, under apache, general.

I went googling for a “Hello World” for the Apache Portable Runtime library, trying to track down a presentation Paul Querna gave at ApacheConEU and was surprised to find that I not only didn’t find his presentation, but couldn’t find any obvious “Hello World” example on-line.

Tracking down Paul’s presentation wasn’t hard, but to try and help anyone else who Google’s for it, here’s the code, taken directly from Paul’s presentation:

#include "apr.h"
#include "apr_file_io.h"
#include <stdlib.h>  /* For atexit() */

int main(int argc, char *argv[])
{
   apr_pool_t *p;
   apr_file_t *fp;
   apr_initialize();
   atexit(apr_terminate);
   apr_pool_create(&p, NULL);
   apr_file_open_stdout(&fp, p);
   apr_file_printf(fp, "Hello World" APR_EOL_STR);
   return 0;
}

While I’m posting it, I might aswell fill in the only blank for total newcomers to APR. Once configured, built and installed, APR will install the apr-1-config utility into $prefix/bin/. This utility tells you the exact arguments you need to use for compiling, linking and so on. For example, to compile helloworld.c;

gcc `apr-1-config --includes  \
    --cflags --cppflags --link-ld --libs`        \
    -o helloworld helloworld.c

Once compiled, you’ll have to make sure that libapr-1.so is in your LD_LIBRARY_PATH or in one of the directories ld.so has been configured to automatically search for libraries. That’s it.

colmmacc@lasaire:~$ ./helloworld
Hello World

If you write C, and you have portability requirements, it might be worth taking a good look at APR. It provides a lot of reliable abstractions for aiding portability, from simple things like a portable getopt to unified file-system and networking layer API’s. It saves me a lot of effort.

4 Replies to "APR Hello World"

gravatar

Fred Ollinger  on January 7, 2007

I had a big problem getting the simplest thing working in apr even after looking at tutorials. This code worked great to get me started. I greatly appreciate that there are people out there who put this thing online. Thanks so much.

Fred

gravatar

Jon Andika  on April 22, 2007

More than 3 hour for me to googling to find the way to start using apr and actually stop until I find this posting. By the way my implementation is in WindowsXP, Eclipse-CDT, Microsoft VC6. Thanks alot

Jon

gravatar

Zaki Mirza  on September 13, 2007

thanks a lot :)

Leave a Comment