我想用C++编写一个Apache模块。我尝试了很准系统模块开始:如何在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
确认没有在mod_example.so
命名example_module
符号。我发现这非常令人困惑,因为如果我手动
gcc -shared -fPIC -DPIC -o mod_example.so `pkg-config --cflags apr-1` -I/usr/include/httpd mod_example.cpp
编译(模仿我看到里面apxs
运行libtool
命令),然后objdump -t
确实在mod_example.so
表现出example_module
符号。
什么给?为什么example_module
出现在我的.so
?我能做些什么来解决它?
如何编译cpp文件到目标文件,然后将目标文件传递到apxs工具? – Ankur
@Ankur太棒了,谢谢你的建议!如果你把这个作为答案,我会接受它;否则我会在一两天内自己写出来接受。 –
Daniel做了@ Ankur的建议工作?如果是这样,请您可以添加关于如何这样做的说明。这可能会帮助我和其他追随你的人。谢谢。 – JulianHarty