我想用 C++ 编写一个 Apache 模块。我尝试了一个非常简单的模块来启动:
#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;
}
编译apxs
似乎工作得很好,使用:
apxs -i -n example_module -c mod_example.cpp
但是,当我尝试启动 httpd 时,出现错误。我插入了一些换行符以使其更清晰。
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
确实,我可以确认objdump -t
没有在 中命名example_module
的符号mod_example.so
。我觉得这特别令人困惑,因为如果我手动编译
gcc -shared -fPIC -DPIC -o mod_example.so `pkg-config --cflags apr-1` -I/usr/include/httpd mod_example.cpp
(模仿我看到libtool
的从内部运行的命令apxs
),然后objdump -t
确实example_module
在mod_example.so
.
是什么赋予了?为什么没有example_module
出现在我的.so
?我能做些什么来修复它?