由于您使用的是C++编译器,函数名称将是
mangled
g++
void initmyModule()
进入之内
_Z12initmyModulev
). 因此,python解释器找不到模块的init函数。
您需要使用普通的C编译器,或者在整个模块中使用
extern "C"
#ifdef __cplusplus
extern "C" {
#endif
#include <Python.h>
static PyObject* py_myFunction(PyObject* self, PyObject* args)
{
char *s = "Hello from C!";
return Py_BuildValue("s", s);
}
static PyMethodDef myModule_methods[] = {
{"myFunction", py_myFunction, METH_VARARGS},
{NULL, NULL}
};
void initmyModule()
{
(void) Py_InitModule("myModule", myModule_methods);
}
#ifdef __cplusplus
}
#endif