我是 Qt 和 C++ 的新手,我想创建一个简单的项目。我写了这段代码:
#include <QString>
int main()
{
QString str = "a,,b,c";
return 0;
}
然后我这样编译它:
g++ -o LeapMouse Leapmouse.cpp -I /home/dougui/Qt/5.1.1/gcc_64/include/QtCore -I /home/dougui/Qt/5.1.1/gcc_64/include -fPIE
我有这个错误:
/tmp/cc6umRmY.o: In function `QString::QString(char const*)':
Leapmouse.cpp:(.text._ZN7QStringC2EPKc[_ZN7QStringC5EPKc]+0x34): undefined reference to `QString::fromAscii_helper(char const*, int)'
/tmp/cc6umRmY.o: In function `QTypedArrayData<unsigned short>::deallocate(QArrayData*)':
Leapmouse.cpp: (.text._ZN15QTypedArrayDataItE10deallocateEP10QArrayData[_ZN15QTypedArrayDataItE10deallocateEP10QArrayData]+0x1e): undefined reference to `QArrayData::deallocate(QArrayData*, unsigned long, unsigned long)'
collect2: error: ld returned 1 exit status
当我执行 grep -ri 'fromAscii_helper'/home/dougui/Qt/5.1.1/gcc_64/include
时,找到了函数,如您所见:
/home/dougui/Qt/5.1.1/gcc_64/include/QtCore/qstring.h: static Data *fromAscii_helper(const char *str, int size = -1);
它应该包括在内。我错过了什么吗?是否可以将 Qt 的库包含到标准 C++ 项目中?
最佳答案
在 C++ 中仅包含相关的 header 是不够的:链接可执行文件时,您还需要指定相关的库以及可能的目录以查找这些库。我不知道 Qt,但根据你上面的编译器调用,我猜你需要添加类似的选项
-L/home/dougui/Qt/5.1.1/gcc_64/lib -lQtCore
第一个选项指定可以在何处找到库,假设默认情况下编译器不会在该目录中查找,第二个选项是定义符号的库的名称。我不知道这个库是如何真正调用的:如果文件是 libQtCore.a
或 libQtCore.so
选项 -lQtCore
添加这个供链接器搜索的库。但是,您可能需要用另一个名称替换 QtCore
。
关于c++ - "undefined reference to ` QString::fromAscii_helper(char const*, int) '",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18796635/