Search code examples
capacheapr

Linker errors in a basic Apache module


I am attempting to write a "hello world" module for Apache HTTPD 2.4. I'm receiving some linker errors when building the module, and many Google searches have not revealed a solution.

My questions are:

  1. How can I resolve the ap_hook_handler linker error? I've searched through the Apache Portable Runtime source and headers and have not found it.
  2. Why is there an undefined reference to main? I thought shared objects don't have a main() entry point.

The source code:

// Dependancies: apr-devel apr-util-devel

#include <httpd.h>
#include <http_config.h>
#include <apr_hooks.h>

static int test_handler(request_rec* request)
{
        return OK;
}

static void register_hooks(apr_pool_t *pool)
{
        ap_hook_handler(test_handler, NULL, NULL, APR_HOOK_LAST);
}

module AP_MODULE_DECLARE_DATA test_module =
{
        STANDARD20_MODULE_STUFF,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        register_hooks
};

The build command:

apxs -lapr-1 -laprutil-1 -c test.c -o mod_test.so

The errors:

apxs -lapr-1 -laprutil-1 -c test.c -o mod_test.so
/usr/lib64/apr-1/build/libtool --silent --mode=compile gcc -prefer-pic -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -Wformat-security -fno-strict-aliasing  -DLINUX=2 -D_REENTRANT -D_GNU_SOURCE -pthread -I/usr/include/httpd  -I/usr/include/apr-1   -I/usr/include/apr-1   -c -o test.lo test.c && touch test.slo
/usr/lib64/apr-1/build/libtool --silent --mode=link gcc -o test.la  -lapr-1 -laprutil-1 -rpath /usr/lib64/httpd/modules -module -avoid-version    test.lo -o mod_test.so
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
.libs/test.o: In function `register_hooks':
/root/test/test.c:14: undefined reference to `ap_hook_handler'
collect2: ld returned 1 exit status
apxs:Error: Command failed with rc=65536

Solution

  • The order matters and the filenames have to go last:

    apxs -c -o mod_test.so test.c
    

    To install it automatically:

    apxs -i -c -o mod_test.so -c test.c
    

    This should work