c++ - 在 Qt 4.5.1 中使用 g++ 的链接器错误

标签 c++ linux qt

我正在尝试测试一个新的开发环境,但在引用一些必需的 Qt 库时遇到了一些问题。

首先我运行了这个:

$ g++ HelloWorld.C -o HelloWorld -I /usr/local/Trolltech/Qt-4.5.1/include/QtCore/ -I /usr/local/Trolltech/Qt-4.5.1/include/

得到这个错误:

    /tmp/ccmsm4kZ.o: In function `QString::QString(char const*)':
HelloWorld.C:(.text._ZN7QStringC2EPKc[_ZN7QStringC5EPKc]+0x1d): undefined reference to `QString::fromAscii_helper(char const*, int)'
/tmp/ccmsm4kZ.o: In function `QString::~QString()':
HelloWorld.C:(.text._ZN7QStringD2Ev[_ZN7QStringD5Ev]+0x2d): undefined reference to `QString::free(QString::Data*)'
collect2: ld returned 1 exit status

然后我通过以下方式添加了对 QtCore 库的引用:

$ g++ HelloWorld.C -o HelloWorld -I /usr/local/Trolltech/Qt-4.5.1/include/QtCore/ -I /usr/local/Trolltech/Qt-4.5.1/include/ -L /usr/local/Trolltech/Qt-4.5.1/lib -lQtCore

这消除了编译错误,但是当我尝试运行该程序时出现此错误:

./HelloWorld: error while loading shared libraries: libQtCore.so.4: cannot open shared object file: No such file or directory

我无法通过谷歌找到解决此问题的方法。有人有什么建议吗?

最佳答案

该错误表明虽然链接器可以在编译时找到该库,但在运行时无法找到它。

您应该像这样更新您的 LD_LIBRARY_PATH 以包含该位置:

~.bashrc 中可能靠近底部:

导出 LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/Trolltech/Qt-4.5.1/lib

或者,如果你想让它在你的整个系统中持久存在(并且有 root 访问权限),你可以在 /etc/ld.so.conf.d 中创建一个条目(在 RedHat 上,我我不确定其他发行版)

touch/etc/ld.so.conf.d/qt.conf

添加这个文件的路径,然后通过/sbin/ldconfig更新你的运行时

关于c++ - 在 Qt 4.5.1 中使用 g++ 的链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23229024/

相关文章:

c++ - 使用带文字的 SysAllocString 有什么问题吗?

c++ - 作为 B 类成员的 A 类如何共享 B 类的私有(private)成员?

linux - 如何在我的 shell 脚本中包含 html?执行 shell 脚本时可以得到 html 格式的输出吗?

c++ - 连接来自另一个在 Qt 中不起作用的类的插槽

c++ - bind() 和 listen() 函数出错 (WinSock)

c++ - 编写 C++ 程序以从 Linux 命令行搜索索引文件

c - 即使在包含 <stdlib.h> 之后,'exit' 也会发出警告

image - Qt:在小部件上显示图像

c++ - 如何在 Visual Studio 2017 中使用 Qt 库?

c++ - 数组访问的返回类型