Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ .so模块不在python中导入:动态模块不定义init函数_C++_Python_C_Python C Api_Python Extensions - Fatal编程技术网

C++ .so模块不在python中导入:动态模块不定义init函数

C++ .so模块不在python中导入:动态模块不定义init函数,c++,python,c,python-c-api,python-extensions,C++,Python,C,Python C Api,Python Extensions,我正在尝试为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

我正在尝试为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);
}
但是,我在尝试导入时出错

$ 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)
$ipython
在[1]中:导入myModule
---------------------------------------------------------------------------
ImportError回溯(最近一次呼叫最后一次)
/()
ImportError:动态模块未定义初始化函数(initmyModule)

为什么不能导入?< /p> < p>因为使用C++编译器,函数名将是(例如,我的代码> g++<代码> Mangles>代码> VoIP InMyMeultMeMe()/<代码> <代码>因此,python解释器将找不到模块的init函数

您需要使用普通C编译器,或通过指令强制整个模块进行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 
\ifdef\uuucplusplus
外部“C”{
#恩迪夫
#包括
/*
*要从Python调用的函数
*/
静态PyObject*py_myFunction(PyObject*self,PyObject*args)
{
char*s=“来自C的你好!”;
返回Py_BuildValue(“s”,s);
}
/*
*将Python函数名绑定到我们的C函数
*/
静态PyMethodDef myModule_方法[]={
{“myFunction”,py_myFunction,METH_VARARGS},
{NULL,NULL}
};
/*
*Python调用这个函数,让我们初始化我们的模块
*/
void initmyModule()
{
(void)Py_InitModule(“myModule”,myModule_方法);
}
#ifdef_uucplusplus
}//外部“C”
#恩迪夫

你的代码似乎有点乱七八糟。@Ignacio:我只是想按照下面的例子来做。有没有一个更简单的例子可以给我指一下?顶部框中的代码真的反映了源文件中的内容吗?@Ignacio。对不起,你说得对。当我从文件中复制它时,它搞砸了。我重新编写了代码。它现在反映了我在源文件中的内容。文档中给出的
PyMODINIT_FUNC
宏将为您处理此问题。@IgnacioVazquez Abrams:您能否详细说明如何执行您的解决方案?谢谢
#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