我正在尝试为python编写C扩展。使用代码(如下),我收到编译器警告:
implicit declaration of function ‘Py_InitModule’
它在运行时因此错误而失败:
undefined symbol: Py_InitModule
我花了几个小时寻找一个没有快乐的解决方案。我尝试过多次语法修改,我甚至发现一篇帖子暗示该方法已被弃用。但是我发现没有替代品。
以下是代码:
#include <Python.h>
//a func to calc fib numbers
int cFib(int n)
{
if (n<2) return n;
return cFib(n-1) + cFib(n-2);
}
static PyObject* fib(PyObject* self,PyObject* args)
{
int n;
if (!PyArg_ParseTuple(args,"i",&n))
return NULL;
return Py_BuildValue("i",cFib(n));
}
static PyMethodDef module_methods[] = {
{"fib",(PyCFunction) fib, METH_VARARGS,"calculates the fibonachi number"},
{NULL,NULL,0,NULL}
};
PyMODINIT_FUNC initcModPyDem(void)
{
Py_InitModule("cModPyDem",module_methods,"a module");
}
如果有帮助的话,我的setup.py:
from distutils.core import setup, Extension
module = Extension('cModPyDem', sources=['cModPyDem.c'])
setup(name = 'packagename',
version='1.0',
description = 'a test package',
ext_modules = [module])
test.py中的测试代码:
import cModPyDem
if __name__ == '__main__' :
print(cModPyDem.fib(200))
任何帮助都会非常感激。
答案 0 :(得分:29)
您拥有的代码在Python 2.x中可以正常工作,但Python 3.x中不再使用Py_InitModule
。现在,您创建了一个PyModuleDef
结构,然后将其引用传递给PyModule_Create
。
结构如下:
static struct PyModuleDef cModPyDem =
{
PyModuleDef_HEAD_INIT,
"cModPyDem", /* name of module */
"", /* module documentation, may be NULL */
-1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
module_methods
};
然后你的PyMODINIT_FUNC
函数看起来像:
PyMODINIT_FUNC PyInit_cModPyDem(void)
{
return PyModule_Create(&cModPyDem);
}
请注意,PyMODINIT_FUNC
函数的名称必须采用PyInit_<name>
格式,其中<name>
是模块的名称。
我认为如果你在Python 3.x文档中阅读Extending是值得的。它详细描述了如何在现代Python中构建扩展模块。
答案 1 :(得分:-1)
我遇到了与Py_InitModule()相同的问题。我从前面提到的Python 3文档开始,特别是&#34;扩展和嵌入Python解释器&#34;文档。但该文档的章节标题为#34;一个简单的例子&#34;遗漏了细节。所以。我用Google搜索了这篇简短的讲座:
http://www.scipy-lectures.org/advanced/interfacing_with_c/interfacing_with_c.html
这在很多方面更适合Python-C API扩展的新用户...除了它没有针对Python v3进行更新。所以...请参阅scipy讲座,Python 3文档以及StackOverflow讨论,根据您的需求剔除每个相关信息。