Why qt project can throw errors like undefined reference to `__imp_ Py_Initialize' and so on?

Question:

I want to add python functions in C++ code.
The project is written using qt.

In Mask.pro I have:

...
INCLUDEPATH += "D:/workplace/Python/include"
LIBS += -L"D:/workplace/Python/libs"
...

In main.cpp I have:

#pragma push_macro("slots")
#undef slots
#include <Python.h>
#pragma pop_macro("slots")
...
PyObject *pName, *pModule, *pFunc;
PyObject *pArgs, *pValue;

Py_Initialize();
pName = PyUnicode_DecodeFSDefault("Morpho");

pModule = PyImport_Import(pName);
pFunc = PyObject_GetAttrString(pModule, 0);

Py_XDECREF(pFunc);
Py_DECREF(pModule);
...

Errors while compiling:

D:workplacePythonincludeobject.h:500: ошибка: undefined reference to `_imp___Py_Dealloc'
debug/main.o: In function `Py_DECREF':
D:/workplace/Python/include/object.h:500: undefined reference to `_imp___Py_Dealloc'
C:ProjectsCprojectQTMaskbuild-Mask-Desktop-Debug..Maskmain.cpp:99: ошибка: undefined reference to `_imp__Py_Initialize'
debug/main.o: In function `Z5qMainiPPc':
C:ProjectsCprojectQTMaskbuild-Mask-Desktop-Debug/../Mask/main.cpp:99: undefined reference to `_imp__Py_Initialize'
C:ProjectsCprojectQTMaskbuild-Mask-Desktop-Debug..Maskmain.cpp:100: ошибка: undefined reference to `_imp__PyUnicode_DecodeFSDefault'
C:ProjectsCprojectQTMaskbuild-Mask-Desktop-Debug..Maskmain.cpp:102: ошибка: undefined reference to `_imp__PyImport_Import'
C:ProjectsCprojectQTMaskbuild-Mask-Desktop-Debug..Maskmain.cpp:103: ошибка: undefined reference to `_imp__PyObject_GetAttrString'

I just can’t find what I am doing wrong or what I even could forget about.


Whole .pro file:

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += 
    QMTIFF.cpp 
    main.cpp 
    mainwindow.cpp 
    processingBlock.cpp

HEADERS += 
    QMTIFF.h 
    mainwindow.h 
    processingBlock.h

FORMS += 
    mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

DISTFILES += 
    Morpho.py

LIBS += -L"D:/workplace/Python/libs" -lpython310

INCLUDEPATH += "D:/workplace/Python/include"
DEPENDPATH += "D:/workplace/Python/include"

LIBS += -L"D:/Qt/Qt5.15.2/5.15.2/Src/qtimageformats/src/3rdparty/libtiff/libtiff" -ltiff

INCLUDEPATH += "D:/Qt/Qt5.15.2/5.15.2/Src/qtimageformats/src/3rdparty/libtiff/libtiff"
DEPENDPATH += "D:/Qt/Qt5.15.2/5.15.2/Src/qtimageformats/src/3rdparty/libtiff/libtiff"

All errors and warnings:

C:ProjectsCprojectQTMaskMaskQMTIFF.cpp:103: предупреждение: comparison between signed and unsigned integer expressions [-Wsign-compare]
..MaskQMTIFF.cpp: In constructor 'QMTIFF::QMTIFF(QString)':
..MaskQMTIFF.cpp:103:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     } while (n < NOfPages);
          ~~^~~~~~~~~~
C:ProjectsCprojectQTMaskMaskQMTIFF.cpp:53: предупреждение: unused parameter 'test' [-Wunused-parameter]
..MaskQMTIFF.cpp:53:24: warning: unused parameter 'test' [-Wunused-parameter]
 QMTIFF::QMTIFF(QString test)
                        ^~~~
C:ProjectsCprojectQTMaskMaskmain.cpp:17: предупреждение: unused variable 'Num' [-Wunused-variable]
..Maskmain.cpp: In function 'int qMain(int, char**)':
..Maskmain.cpp:17:18: warning: unused variable 'Num' [-Wunused-variable]
     unsigned int Num = 1190;
                  ^~~
C:ProjectsCprojectQTMaskMaskmain.cpp:25: предупреждение: unused variable 'from' [-Wunused-variable]
..Maskmain.cpp:25:9: warning: unused variable 'from' [-Wunused-variable]
     int from = 1001;
         ^~~~
C:ProjectsCprojectQTMaskMaskmain.cpp:26: предупреждение: unused variable 'to' [-Wunused-variable]
..Maskmain.cpp:26:9: warning: unused variable 'to' [-Wunused-variable]
     int to  =  1190;
         ^~
C:ProjectsCprojectQTMaskMaskmain.cpp:96: предупреждение: unused variable 'pArgs' [-Wunused-variable]
..Maskmain.cpp:96:15: warning: unused variable 'pArgs' [-Wunused-variable]
     PyObject *pArgs, *pValue;
               ^~~~~
C:ProjectsCprojectQTMaskMaskmain.cpp:96: предупреждение: unused variable 'pValue' [-Wunused-variable]
..Maskmain.cpp:96:23: warning: unused variable 'pValue' [-Wunused-variable]
     PyObject *pArgs, *pValue;
                       ^~~~~~
D:workplacePythonincludeobject.h:500: ошибка: undefined reference to `_imp___Py_Dealloc'
debug/main.o: In function `Py_DECREF':
D:/workplace/Python/include/object.h:500: undefined reference to `_imp___Py_Dealloc'
C:ProjectsCprojectQTMaskbuild-Mask-Desktop-Debug..Maskmain.cpp:98: ошибка: undefined reference to `_imp__Py_Initialize'
debug/main.o: In function `Z5qMainiPPc':
C:ProjectsCprojectQTMaskbuild-Mask-Desktop-Debug/../Mask/main.cpp:98: undefined reference to `_imp__Py_Initialize'
C:ProjectsCprojectQTMaskbuild-Mask-Desktop-Debug..Maskmain.cpp:99: error: undefined reference to `_imp__PyUnicode_DecodeFSDefault'
C:ProjectsCprojectQTMaskbuild-Mask-Desktop-Debug..Maskmain.cpp:101: error: undefined reference to `_imp__PyImport_Import'
C:ProjectsCprojectQTMaskbuild-Mask-Desktop-Debug..Maskmain.cpp:102: ошибка: undefined reference to `_imp__PyObject_GetAttrString'
:-1: error: collect2.exe: error: ld returned 1 exit status
Asked By: Gorbatovsky

||

Answers:

Python (at least the official binaries) for Win is built using VStudio ([Python.Wiki]: WindowsCompilers).
According to an output line added after a while (:-1: error: collect2.exe: error: ld returned 1 exit status), it seems a different build toolchain (looks like a port from Nix (MinGW, MSYS2, Cygwin, …)) is used.

It’s generally advisable not to mix build toolchains, as that could yield errors (at build time, or (even worse) crashes at runtime). Check [SO]: How to circumvent Windows Universal CRT headers dependency on vcruntime.h (@CristiFati’s answer) for a remotely related example.

Try setting VStudio (might need to install it – Community Edition is free) in your .pro file, like in the image below (don’t mind the older version, this is what I have configured already):

Img00


This (original answer) is a generic advice and is not the cause for the current failure, as the library is automatically included (via pyconfig.h).

You specified where the linker should search for libraries, but you didn’t specify which libraries to use. Check [SO]: How to include OpenSSL in Visual Studio (@CristiFati’s answer) for more details on building on Win.

Inside Python‘s lib directory ("D:/workplace/Python/libs" in your case), there should be a file called python${PYTHON_MAJOR}${PYTHON_MINOR}.lib depending on which Python version you are using (e.g. python39.lib for Python 3.9). You need to let the linker know about it:

LIBS += -L"D:/workplace/Python/libs" -lpython39

Note: On Win you don’t have to separately install Python packages (python*, libpython*, libpython*-dev, …) as they are all contained by the (official) installer.

Answered By: CristiFati
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.