3

I'd like to include Python.h (from the Python distribution in my Anaconda folder) in my project to call a python script. The program compiles fine when I don't include python. But as soon as I do, I get undefined reference errors to functions implemented in Qt classes (so not my own functions!). The python version I'd like to include is 3.5.5.

The part that confuses me most is undefined reference to QJsonValue::toString(). This method is implemented inline so how can its implementation not be found? According to QtCreator the problem originates in a compiled object that tries to call this function.

This is a minimally (not) working example:

The .pro file:

QT -= gui
CONFIG += c++11 console no_keywords
SOURCES += main.cpp
INCLUDEPATH += {path to python include}
LIBS += -L{path to python lib} -lpython3.5m

And the main.cpp file:

#include <Python.h>
#include <QCoreApplication>
#include <QJsonValue>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    PyObject *obj;
    QJsonValue value;
    value.toString();
    return a.exec();
}

Update: It seems like including Python from Anaconda is causing the issue. When I remove LIBS += -L{path to python lib} -lpython3.5m it compiles just fine. And when I remove INCLUDEPATH += {path to python include} but keep the other line I get the following error:

/{user}/build-TestProject-Qt_5_9-Debug/TestProject: /{user}/anaconda3/lib/libQt5Core.so.5: version `Qt_5.9' not found (required by /{user}/build-TestProject-Qt_5_9-Debug/TestProject)
Florian Blume
  • 3,237
  • 17
  • 37
  • Have you tried to `#include ` after the `#include `? Python header may be messing up something with the preprocessor. – Velkan Jul 09 '18 at 06:31
  • Yes, no luck either. – Florian Blume Jul 09 '18 at 08:08
  • QtCreator is an IDE, so “according to QtCreator” should really be “according to linker console output”. The problem may we’ll be that “Python.h” pollutes the global namespace. Try compiling something with “Python.h” in it and no references to Qt in the source code, I.e. add “mypy.cpp” to the project, and use some Python API there. Expose the interface to it in “mypy.h”, then use it from `main()`. – Kuba hasn't forgotten Monica Jul 09 '18 at 10:13
  • Of course you're right, it's the linker output. I actually know that don't know why I wrote QtCreator. Unfortunately your idea didn't work. I even tried not importing the class that imports Python.h. The main.cpp still doesn't compile. Apparently it's the two lines in the .pro file (see edit). – Florian Blume Jul 09 '18 at 10:51
  • Ok I solved it. The problem was that QtCreator wanted qmake 5.9 like defined in my kit, but including Python.h from Anaconda used qmake 5.6 from the bin folder of Anaconda. I added the 5.6 kit and it works now. Although this seems like an ugly solution. Should I edit and answer the question or delete it? – Florian Blume Jul 09 '18 at 11:03
  • @HappyFeet do not edit your question, you must publish an answer. – eyllanesc Jul 09 '18 at 11:10
  • No I mean as to include the information, that I used the Anaconda Python distribution. This is a crucial fact. I included it in the question. – Florian Blume Jul 09 '18 at 11:13

1 Answers1

1

The issue was caused by using Anaconda's Python distribution. Setting the project to include Python causes QtCreator to use Anaconda's qmake instead of the installed version. If you can live with Qt 5.6, which is the current version of Qt in Anaconda, create a Kit with Anaconda's qmake and the program compiles again.

If you need a newer version of Qt you can add the line

-L/{user}/Qt5.9.5/5.9.5/gcc_64/lib -lQt5Core

to your .pro file. Adjust it to your Qt version and what libraries you need. This is not exactly a pretty solution, as you need to adjust the .pro file whenever you want to switch versions, but it's the only solution I know of.

Florian Blume
  • 3,237
  • 17
  • 37