未定义的符号:Ubuntu 10.10中的Py_Initialize

时间:2011-12-13 15:22:57

标签: python c apache2

我使用Python在C API中创建了自己的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 个答案:

答案 0 :(得分: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选项进行编译,则可能会产生一些抱怨;我没试过。