How can I write an Apache module in C++?

2019-04-11 21:57发布

问题:

I'd like to write an Apache module in C++. I tried a very barebones module to start:

#include "httpd.h"
#include "http_core.h"
#include "http_protocol.h"
#include "http_request.h"

static void register_hooks(apr_pool_t *pool);
static int example_handler(request_rec *r);

extern "C" module example_module;

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

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

static int example_handler(request_rec *r) {
    if (!r->handler || strcmp(r->handler, "example"))
        return (DECLINED);

    ap_set_content_type(r, "text/plain");
    ap_rputs("Hello, world!", r);
    return OK;
}

Compiling with apxs seems to work just fine, using:

apxs -i -n example_module -c mod_example.cpp

However, when I try to start httpd, I get an error. I've inserted some newlines to make it more legible.

httpd: Syntax error on line 56 of /etc/httpd/conf/httpd.conf:
       Syntax error on line 1 of /etc/httpd/conf.modules.d/20-mod_example.conf:
       Can't locate API module structure `example_module' in file /etc/httpd/modules/mod_example.so:
       /etc/httpd/modules/mod_example.so: undefined symbol: example_module

Indeed, I can confirm with objdump -t that there is no symbol named example_module in mod_example.so. I find this especially confusing because if I manually compile with

gcc -shared -fPIC -DPIC -o mod_example.so `pkg-config --cflags apr-1` -I/usr/include/httpd mod_example.cpp

(which mimics the command I see libtool running from inside apxs), then objdump -t does indeed show an example_module symbol in mod_example.so.

What gives? Why doesn't example_module appear in my .so? What can I do to fix it?

回答1:

One approach to solve this problem would be to compile the cpp file to object file and then pass that object file to the apxs tool. For example:

g++ `pkg-config --cflags apr-1` -fPIC -DPIC -c mod_example.cpp
apxs -i -n example_module `pkg-config --libs apr-1` -c mod_example.o