0

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
Ryan M
  • 18,333
  • 31
  • 67
  • 74

1 Answers1

0

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.

CristiFati
  • 38,250
  • 9
  • 50
  • 87
  • Thanks alot for your answer, but even after redacting .pro file I have the same problem In Mask.pro I have: ... LIBS += -L"D:/workplace/Python/libs" -lpython310 INCLUDEPATH += "D:/workplace/Python/include" DEPENDPATH += "D:/workplace/Python/include" ... – Gorbatovsky May 02 '22 at 07:44
  • How do you build it? From Creator? – CristiFati May 02 '22 at 08:28
  • Yes, I build it from Qt Creator – Gorbatovsky May 02 '22 at 09:48
  • What other reasons for these errors could be? Addition: Another library has also been included in the .pro file as follows: ... 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" ... This lib works correctly. – Gorbatovsky May 02 '22 at 12:48
  • Wait a minute.... What I stated in my answer is a generic solution, but for *Python* it isn't necessary, as the lib is included in one of the header filers. How did you install *D:/workplace/Python*? Is there any *D:/workplace/Python/libs/python310.lib*? – CristiFati May 02 '22 at 13:49
  • Python, as far as I remember, was installed from python.org There are next files in libs: _tkinter.lib python3.lib python310.lib – Gorbatovsky May 02 '22 at 14:33
  • Please post the whole *.pro* file. Don't you also have in the *Compile Output* tab (in the lower window part) something like: *warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'*? – CristiFati May 02 '22 at 14:35
  • And in the D:/workplace/Python/include we have lots of .h files – Gorbatovsky May 02 '22 at 14:37
  • After all errors compiler throws this: collect2.exe: error: ld returned 1 exit status Whole .pro file is in the new answer. – Gorbatovsky May 02 '22 at 14:47
  • Don't add extra data in answers, edit the question instead. Also please add the full build log including the compile and link commands outputs errors. and warnings – CristiFati May 02 '22 at 20:12
  • I understand that it is not correct to do this, but, unfortunately, when creating a question, it is not allowed to fill it with so much code and so few comments. – Gorbatovsky May 02 '22 at 22:57
  • All errors and warnings are added to the comment – Gorbatovsky May 02 '22 at 23:06