在Qt应用程序中包含Python.h会导致对Qt函数的未定义引用 [英] Including Python.h in Qt application causes undefined reference to Qt functions

查看:579
本文介绍了在Qt应用程序中包含Python.h会导致对Qt函数的未定义引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在项目中包含Python.h(来自Anaconda文件夹中的Python发行版)以调用python脚本.当我不包含python时,程序可以正常编译.但是,一旦这样做,我就会在Qt类中实现的函数出现undefined reference错误(因此不是我自己的函数!).我想包含的python版本是3.5.5.

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.

最让我困惑的部分是undefined reference to QJsonValue::toString().此方法是内联实现的,那么如何找不到它的实现?根据QtCreator的说法,问题出在试图调用此函数的已编译对象上.

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:

.pro文件:

QT -= gui
CONFIG += c++11 console no_keywords
SOURCES += main.cpp
INCLUDEPATH += {path to python include}
LIBS += -L{path to python lib} -lpython3.5m

和main.cpp文件:

And the main.cpp file:

#include <Python.h>
#include <QCoreApplication>
#include <QJsonValue>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    PyObject *obj;
    QJsonValue value;
    value.toString();
    return a.exec();
}

更新:似乎包括Anaconda的Python引起了此问题.当我删除LIBS += -L{path to python lib} -lpython3.5m时,它可以正常编译.当我删除INCLUDEPATH += {path to python include}但保留另一行时,出现以下错误:

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)

推荐答案

问题是由使用Anaconda的Python发行版引起的.将项目设置为包括Python会使QtCreator使用Anaconda的qmake而不是已安装的版本.如果您可以使用Anaconda中Qt的当前版本Qt 5.6,请使用Anaconda的qmake创建一个工具包,然后程序将再次编译.

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.

如果您需要更新版本的Qt,可以添加该行

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

到您的.pro文件.将其调整为您的Qt版本以及所需的库.这并不是一个很好的解决方案,因为每当您要切换版本时都需要调整.pro文件,但这是我所知道的唯一解决方案.

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.

这篇关于在Qt应用程序中包含Python.h会导致对Qt函数的未定义引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆