最近在Qt里调用python代码,参考教程(https://blog.youkuaiyun.com/a137748099/article/details/119217197)引入python的include、libs之后,在c++里写了简单的调用python的代码:
// 测试python
cout<<"hello, c++\n";
Py_Initialize();
PyRun_SimpleString("import sys"); // add 1
PyRun_SimpleString("sys.path.append('./')"); //add 2
PyRun_SimpleString("print('hello, python')");
Py_Finalize();
运行就一直报下面的各种错,在网上搜索解决方案后解决了一个又出现另一个错:
Fatal Python error: Py_Initialize: unable to load
error: '::hypot' has not been declared using ::hypot; ^
error: undefined reference to `_imp__Py_Initialize'
\include\object.h:448: error: expected unqualified-id before ‘;’ token
PyType_Slot slots; / terminated by slot==0. */
最后参考(https://blog.youkuaiyun.com/u014489596/article/details/42124595)试了一下将python换成了32
位的(我的Qt用的32位的而且不支持64位的MinGW),上面的报错就都解决了。
一开始也有考虑过是32位c++和64位python不匹配的问题,但是由于我要调用的python代码是用64位解释器写好的,如果把python改成32位的话很可能导致写好的python代码用不了,所以就没往这个角度来解决,企图通过修改c++上的东西来解决问题。唉,该来的总要来。。。