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

只谈情不闲聊 提交于 2019-12-06 07:03:21

问题


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


来源:https://stackoverflow.com/questions/42605138/how-can-i-write-an-apache-module-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!