简体   繁体   English

如何在C ++中编写Apache模块?

[英]How can I write an Apache module in C++?

I'd like to write an Apache module in C++. 我想用C ++编写一个Apache模块。 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编译似乎工作正常,使用:

apxs -i -n example_module -c mod_example.cpp

However, when I try to start httpd, I get an error. 但是,当我尝试启动httpd时,我收到错误。 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 . 实际上,我可以用objdump -t确认mod_example.so没有名为example_modulemod_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 . (它模仿我看到libtoolapxs内部运行的命令),然后objdump -t确实在mod_example.so显示了一个example_module符号。

What gives? 是什么赋予了? Why doesn't example_module appear in my .so ? 为什么我的.so中没有出现example_module What can I do to fix it? 我该怎么办才能修复它?

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. 解决此问题的一种方法是将cpp文件编译为目标文件,然后将该目标文件传递给apxs工具。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM