如何用C ++编写Apache模块? [英] How can I write an Apache module in C++?

查看:148
本文介绍了如何用C ++编写Apache模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用C ++编写一个Apache模块.我尝试了一个非常准系统的模块来开始:

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;
}

使用apxs进行编译似乎可以正常工作,使用:

Compiling with apxs seems to work just fine, using:

apxs -i -n example_module -c mod_example.cpp

但是,当我尝试启动httpd时,出现错误.我插入了一些新行以使其更清晰.

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

实际上,我可以用objdump -t确认在mod_example.so中没有名为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

(模仿我在apxs内部看到的libtool命令),然后objdump -t确实在mod_example.so中显示了example_module符号.

(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.

有什么作用?为什么example_module没有出现在我的.so中?我该怎么解决?

What gives? Why doesn't example_module appear in my .so? What can I do to fix it?

推荐答案

解决此问题的一种方法是将cpp文件编译为目标文件,然后将该目标文件传递给apxs工具.例如:

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

这篇关于如何用C ++编写Apache模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆