我正在尝试测试一个新的开发环境,但在引用一些必需的 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/