Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Python.h of python 3.2 must be included as first together with Qt4

Tags:

c++

python

qt

I have a qt application and I want to implement python interpreter into it so that I can extend it with python scripts. While this works fine for regular C++ application, including Python.h even for most simple, empty Qt4 project always result in:

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

I only implemented this in my .pro file:

INCLUDEPATH += "/usr/include/python3.2"

now anytime when I do

#include <Python.h>

in any .h file it makes it unbuildable. Why is that?

Note: This all works perfectly with python 2.7, just python 3x doesn't work

EDIT: I figured out, that when I include Python.h as first file, before Qt includes, it works, is this a bug in python? Are they missing some safe guards?

like image 563
Petr Avatar asked Nov 01 '22 09:11

Petr


1 Answers

The documentation of the Python C-API states:

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.

It is very likely that some of the Qt headers include standard headers (as evident from the error you get, it does include /usr/include/features.h, or example), therefore #include <Python.h> should be placed before the Qt headers. In fact, it should generally be placed before any other include-statement.

Note that this is the case with Python 2.7, too. If a different include order works for you with Python 2.7, then you are simply lucky.

like image 118
jogojapan Avatar answered Nov 09 '22 23:11

jogojapan