如何用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; }
使用如何用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; },c++,apache,apache-modules,apxs2,C++,Apache,Apache Modules,Apxs2,使用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:
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\u example.so
中没有名为example\u module
的符号。我发现这尤其令人困惑,因为如果我用
gcc -shared -fPIC -DPIC -o mod_example.so `pkg-config --cflags apr-1` -I/usr/include/httpd mod_example.cpp
(这模仿了我看到的libtool
从apxs
内部运行的命令),然后objdump-t
确实在mod\u示例中显示了一个example\u模块
符号
有什么好处?为什么我的中没有出现示例\u模块
。所以
?我可以做些什么来修复它?解决此问题的一种方法是将cpp文件编译为对象文件,然后将该对象文件传递给apxs工具。例如:
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
如何将cpp文件编译为对象文件,然后将对象文件传递给apxs工具?@Ankur Fantastic,谢谢您的建议!如果你这样回答,我会接受的;否则我会在一两天内自己写出来接受。丹尼尔的建议奏效了吗?如果是的话,请你们中的任何一位加上一条关于如何做的说明。这可能会帮助我和其他跟踪你的人。谢谢。@JulianHarty当然可以,我今天到我的工作笔记本电脑时会补充一些细节。@JulianHarty完成了。