【发布时间】:2015-03-18 09:57:36
【问题描述】:
我正在尝试重现以下教程https://csl.name/post/c-functions-python/。
我在 C++ 中的 Python 扩展如下所示:
#include <Python.h>
static PyObject* py_myFunction(PyObject* self, PyObject* args)
{
char *s = "Hello from C!";
return Py_BuildValue("s", s);
}
static PyObject* py_myOtherFunction(PyObject* self, PyObject* args)
{
double x, y;
PyArg_ParseTuple(args, "dd", &x, &y);
return Py_BuildValue("d", x*y);
}
static PyMethodDef extPy_methods[] = {
{"myFunction", py_myFunction, METH_VARARGS},
{"myOtherFunction", py_myOtherFunction, METH_VARARGS},
{NULL, NULL}
};
void initextPy(void)
{
(void) Py_InitModule("extPy", extPy_methods);
}
我正在使用以下 setup.py:
from distutils.core import setup, Extension
setup(name='extPy', version='1.0', \
ext_modules=[Extension('extPy', ['extPy.cpp'])])
使用python setup.py install 调用它后,.so 文件似乎在正确的位置。
但是当我尝试将此扩展程序与 import extPy 一起使用时,我得到了错误:
ImportError: 动态模块没有定义初始化函数
我在这里缺少什么?感谢您的帮助。
【问题讨论】: