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

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


Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1