在Ubuntu 10.10中出现未定义符号:Py_Initialize

0 投票
1 回答
1249 浏览
提问于 2025-04-17 08:18

我用C语言和Python创建了自己的Apache模块。

#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"
#include "ap_config.h"
#include "python2.7/Python.h"
static char* a(){
Py_Initialize();
  PyRun_SimpleString("from time import time,ctime\n"
                     "print 'Today is',ctime(time())\n");
  Py_Finalize();
return "aba\t";
}
/* The sample content handler */
static int mor_handler(request_rec *r)
{
    if (strcmp(r->handler, "mor")) {
        return DECLINED;
    }
    r->content_type = "text/html";      

    if (!r->header_only)
     {  char *d;
     d=  a();
ap_rputs(d, r);
        ap_rputs("The sample page from mod_mor.c\n", r);}
    return OK;
}

static void mor_register_hooks(apr_pool_t *p)
{
    ap_hook_handler(mor_handler, NULL, NULL, APR_HOOK_MIDDLE);
}

/* Dispatch list for API hooks */
module AP_MODULE_DECLARE_DATA mor_module = {
    STANDARD20_MODULE_STUFF, 
    NULL,                  /* create per-dir    config structures */
    NULL,                  /* merge  per-dir    config structures */
    NULL,                  /* create per-server config structures */
    NULL,                  /* merge  per-server config structures */
    NULL,                  /* table of config file commands       */
    mor_register_hooks  /* register hooks                      */
};

但是出现了一个错误。我不太明白这个错误的意思。

/etc/init.d/apache2 restart 
apache2: Syntax error on line 203 of /etc/apache2/apache2.conf: Syntax error on line 1 of /etc/apache2/mods-enabled/mor.load: Cannot load /usr/lib/apache2/modules/mod_mor.so into server: /usr/lib/apache2/modules/mod_mor.so: `undefined symbol: Py_Initialize`
`Action 'configtest' failed`.
The Apache error log may have more information.
       ...fail!

1 个回答

1

你可以添加对libpython的引用:

$ apxs2 -cia mod_mor.c -lpython2.7

这样你就应该能够加载你的模块。如果你不能引用Python的动态库(.so文件),那么你可以尝试使用库的.a版本:

$ apxs2 -cia mod_mor.c -Wl,-static -lpython2.7 

不过,如果libpython2.7.a中的对象没有使用-fPIC选项编译,可能会出现一些问题;我还没有尝试过。

撰写回答