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.
CodePudding user response:
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.
CodePudding user response:
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"