qt调用python函数_在Qt应用程序中包含Python.h导致对Qt函数的未定义引用

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

#include

#include

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)

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值