环境配置
(1)python的lib 以及头文件分别加到vc的include/lib directories中去。
(2)写完编译后出现找不到python33_d.lib 错误,出现这个错误的原因是:python_d.lib是 库的调试后形式,当我们以debug模式编译工程时,python就用这个lib文件,但是这个文件是不可用 的。对于这点,最快的办法就是强制要求python在任何情况下都是用非调试版本,就可以了。
所以要做的是:
- 修改python头文件pyconfig.h的配置,修改指向调试库文件的参数,由
修改为:# if defined(_DEBUG) # pragma comment(lib,"python33_d.lib") # elif defined(Py_LIMITED_API) # pragma comment(lib,"python3.lib") # else # pragma comment(lib,"python33.lib") # endif /* _DEBUG */
# if defined(_DEBUG) # pragma comment(lib,"python33.lib") # elif defined(Py_LIMITED_API) # pragma comment(lib,"python3.lib") # else # pragma comment(lib,"python33.lib") # endif /* _DEBUG */
(3)在Visual Studio中编译时必须注意,若Python为64bit而VS C++ Project Config里的platform是32bit,会报错:
Error 1 error LNK2019: unresolved external symbol __imp__Py_Finalize referenced in function _main
Error 2 error LNK2019: unresolved external symbol __imp__PyEval_CallObjectWithKeywords referenced in function _main
Error 3 error LNK2019: unresolved external symbol __imp__PyObject_GetAttrString referenced in function _main
Error 4 error LNK2019: unresolved external symbol __imp__PyImport_ImportModule referenced in function _main
Error 5 error LNK2019: unresolved external symbol __imp__Py_Initialize referenced in function _main
Error 6 error LNK1120: 5 unresolved externals
注意修改自己的platform
补充:
error LNK2001: unresolved external symbol __imp__Py_RefTotal
error LNK2019: unresolved external symbol __imp__Py_NegativeRefcount referenced in function _SWIG_Py_None
error LNK2019: unresolved external symbol __imp__PyObject_DebugFree referenced in function SwigPyObject_dealloc
error LNK2019: unresolved external symbol __imp__PyObject_DebugMalloc referenced in function SwigPyObject_New
error LNK2019: unresolved external symbol __imp_PyModule_Create2TraceRefs referenced in function PyInit__wrap
出现这个错误,只需将pyconfig.h中
#ifdef _DEBUG
# define Py_DEBUG
#endif
改为
#ifdef _DEBUG
//# define Py_DEBUG
#endif