Qt:尝试在qt中使用python.h时未定义的符号

时间:2017-05-06 00:01:23

标签: python c++ macos qt compiler-errors

我在Mac上用Qt做了一些小工作。我需要在main函数中调用python函数。但是当我尝试按照每个人的说法调用Py_initialize()时,编译器会抛出这样的错误:

Undefined symbols for architecture x86_64:
  "_Py_Initialize", referenced from:
  _main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see 
invocation)
make: *** [video.app/Contents/MacOS/video] Error 1
07:48:07: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project video (kit: Desktop Qt 5.8.0 
clang 64bit)
When executing step "Make"

我的代码很简单,不应该有任何奇怪的事情。这是:

// bunch of headers
#include <python2.7/Python.h>

using namespace std;


int main(int argc, char *argv[])
{

Py_Initialize();

QGuiApplication app(argc, argv);

QQmlApplicationEngine engine;

QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:///main.qml")));
QObject *object = component.create();
QObject *mainForm = object->findChild<QObject*>("mainForm");

SignalNexus sn;

QObject::connect(mainForm, SIGNAL(populateToken(QString)), &sn, SLOT(pushToPipe(QString)));

QObject::connect(&sn, SIGNAL(populateResult(QVariant, QVariant)), mainForm, SLOT(handleResult(QVariant, QVariant)));
return app.exec();
}

当我评论Py_initalize()行时,一切正常。我认为这与OSx有关。但我真的不知道如何解决这个问题。需要帮助。

1 个答案:

答案 0 :(得分:1)

将您的包含行更改为此

// bunch of headers
 #include <Python/Python.h>

using namespace std;


int main(int argc, char *argv[])
{

Py_Initialize();