我有一个 qt 应用程序,我想在其中实现 python 解释器,以便我可以使用 python 脚本扩展它。虽然这适用于常规 C++ 应用程序,包括 Python.h,但即使对于最简单的空 Qt4 项目,也总是会导致:
g++ -c -m64 -pipe -O2 -Wall -W -D_REENTRANT -DQT_WEBKIT -DQT_NO_DEBUG -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++-64 -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4 -I/usr/include/python3.2mu -I. -o main.o main.cpp
In file included from /usr/include/python3.2mu/Python.h:8:0,
from main.cpp:16:
/usr/include/python3.2mu/pyconfig.h:1182:0: warning: "_POSIX_C_SOURCE" redefined [enabled by default]
/usr/include/features.h:164:0: note: this is the location of the previous definition
/usr/include/python3.2mu/pyconfig.h:1204:0: warning: "_XOPEN_SOURCE" redefined [enabled by default]
/usr/include/features.h:166:0: note: this is the location of the previous definition
In file included from /usr/include/python3.2mu/Python.h:67:0,
from main.cpp:16:
/usr/include/python3.2mu/object.h:402:23: error: expected unqualified-id before ‘;’ token
make: *** [main.o] Error 1
我只在我的 .pro 文件中实现了这个:
INCLUDEPATH += "/usr/include/python3.2"
现在任何时候我做
#include <Python.h>
在任何 .h 文件中,它都无法构建。这是为什么?
注意:这一切都适用于 python 2.7,只是 python 3x 不起作用
编辑:我想通了,当我将 Python.h 作为第一个文件包含时,在 Qt 包含之前,它起作用了,这是 python 中的错误吗?他们是否缺少一些安全 guard ?
最佳答案
documentation Python C-API 声明:
Note Since Python may define some pre-processor definitions which affect the standard headers on some systems, you must include Python.h before any standard headers are included.
某些 Qt header 很可能包含标准 header (从您收到的错误中可以明显看出,它确实包含 /usr/include/features.h
或示例),因此 #include <Python.h>
应该放在 Qt 头文件之前。事实上,它通常应该放在任何其他包含语句之前。
请注意,Python 2.7 也是如此。如果不同的包含顺序适用于 Python 2.7,那么您简直太幸运了。
关于c++ - 为什么python 3.2的Python.h必须和Qt4一起先包含进来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20300201/