.so模块在Python中无法导入:动态模块未定义init函数
我正在尝试为一个C语言的函数写一个Python的封装器。写完所有代码并成功编译后,Python却无法导入这个模块。我在这里跟着一个例子在做,链接在这里。我把这个例子复制过来,并修正了一些拼写错误。这个文件叫做myModule.c:
#include <Python.h>
/*
* Function to be called from Python
*/
static PyObject* py_myFunction(PyObject* self, PyObject* args)
{
char *s = "Hello from C!";
return Py_BuildValue("s", s);
}
/*
* Bind Python function names to our C functions
*/
static PyMethodDef myModule_methods[] = {
{"myFunction", py_myFunction, METH_VARARGS},
{NULL, NULL}
};
/*
* Python calls this to let us initialize our module
*/
void initmyModule()
{
(void) Py_InitModule("myModule", myModule_methods);
}
因为我在使用Mac和Macports的Python,所以我这样编译它:
$ g++ -dynamiclib -I/opt/local/Library/Frameworks/Python.framework/Headers -lpython2.6 -o myModule.dylib myModule.c
$ mv myModule.dylib myModule.so
但是,当我尝试导入它时却出现了错误。
$ ipython
In[1]: import myModule
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
/Users/.../blahblah/.../<ipython console> in <module>()
ImportError: dynamic module does not define init function (initmyModule)
为什么我无法导入它呢?
1 个回答
5
因为你在使用C++编译器,所以函数的名字会被“搞乱”,这叫做名字修饰(mangled)。比如,我的g++
会把void initmyModule()
这个函数名变成_Z12initmyModulev
。所以,Python解释器找不到你模块的初始化函数。
你需要要么使用普通的C编译器,要么在你的模块中强制使用C语言的链接方式,这可以通过一个叫extern "C"
的指令来实现:
#ifdef __cplusplus
extern "C" {
#endif
#include <Python.h>
/*
* Function to be called from Python
*/
static PyObject* py_myFunction(PyObject* self, PyObject* args)
{
char *s = "Hello from C!";
return Py_BuildValue("s", s);
}
/*
* Bind Python function names to our C functions
*/
static PyMethodDef myModule_methods[] = {
{"myFunction", py_myFunction, METH_VARARGS},
{NULL, NULL}
};
/*
* Python calls this to let us initialize our module
*/
void initmyModule()
{
(void) Py_InitModule("myModule", myModule_methods);
}
#ifdef __cplusplus
} // extern "C"
#endif