C++开发python扩展模块:ImportError: dynamic module does not define init function (initRabbit)解决办法

原文首发地址:http://www.atolin.net/c%E5%BC%80%E5%8F%91python%E6%89%A9%E5%B1%95%E6%A8%A1%E5%9D%97%EF%BC%9Aimporterror-dynamic-module-does-not-define-init-function-initrabbit%E8%A7%A3%E5%86%B3%E5%8A%9E%E6%B3%95/


用C++为Python编写扩展模块(动态链接库),并在Python中调用C++开发的扩展功能函数过程,遇到如下错误的童鞋,系不系很苦恼啊:

 ImportError: dynamic module does notdefine init function (initRabbit)

经过分析,原因是,没有正确导出封装python扩展模块所必需的init函数,正确的导出方法为:

#ifdef PYRABBIT_EXPORTS

#define PYRABBIT_API __declspec(dllexport)

#else

#define PYRABBIT __declspec(dllimport)

#endif

 

extern "C"  PYRABBIT_APIvoid initRabbit(){

    PyObject* m=Py_InitModule("Rabbit",RabbitMethods);

}

 

上面的initRabbit()函数的声明定义,是关键。是不是豁然开朗迎刃而解呢。

你可能感兴趣的:(C++,api,python,function,Module,扩展)