链接到 Qt4 库

Posted

技术标签:

【中文标题】链接到 Qt4 库【英文标题】:Linking to Qt4 library 【发布时间】:2017-06-01 19:33:45 【问题描述】:

我正在尝试编译一个需要 Qt4 的简短测试程序,但无法正确。我已经通过

安装了Qt4

sudo apt-get install qt4-dev-tools

程序代码如下所示

#include <QtCore>
#include <iostream>

int main() 

    std::cout << "Qt version: " << qVersion() << std::endl;

共享库 libQtCore.so 位于 /usr/lib/x86_64-linux-gnu 但尝试按以下方式编译

g++ -L/usr/lib/x86_64-linux-gnu -Wall -o test.exe test.cpp -lQtCore

返回一条错误消息,指出没有名为 QtCore 的文件或目录。

我也试过直接使用QtCore源码,但是收到如下错误信息:

/tmp/ccljEHOY.o: 在函数main': test.cpp:(.text+0xa): undefined reference toqVersion()' collect2:错误:ld 返回 1 个退出状态

我在这里做错了什么?

谢谢

Ips

【问题讨论】:

【参考方案1】:

编译器找不到QtCore文件,因为您需要将带有Qt的路径添加到要搜索头文件的目录列表中。

您可以使用pkg-config 获取正确的标志:

$:pkg-config QtCore --cflags
-DQT_SHARED -I/usr/include/qt4 -I/usr/include/qt4/QtCore

对于链接:

$:pkg-config QtCore --libs
-lQtCore

调用编译器时可以使用pkg-config

g++ test.cpp `pkg-config QtCore --cflags --libs`

g++ `pkg-config QtCore --cflags` test.cpp `pkg-config QtCore --libs`

请注意,以下方式不起作用:

g++ `pkg-config QtCore --cflags --libs` test.cpp

【讨论】:

以上是关于链接到 Qt4 库的主要内容,如果未能解决你的问题,请参考以下文章