c++ - "undefined reference to ` QString::fromAscii_helper(char const*, int) '"

标签 c++ qt

我是 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.alibQtCore.so 选项 -lQtCore 添加这个供链接器搜索的库。但是,您可能需要用另一个名称替换 QtCore

关于c++ - "undefined reference to ` QString::fromAscii_helper(char const*, int) '",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18796635/

相关文章:

c++ - WTL头文件如何实现?

c++ - 为什么编译器接受这个删除的 move 构造函数?

c++ - Qt QVBoxLayout : How to divide the layout in fixed height Boxes?

C++ 表达式作为变量

c++ - 为什么 const_cast 删除指针的 constness 而不是指向 const 的指针?

c++ - 连接两个具有不同参数的信号

linux - 在嵌入式 linux 平台上使用 QT 运行在 Windows 中创建的应用程序

XCode 8 错误 "Project ERROR: Xcode not set up properly",即使许可证已被接受

c++ - 对于给定的数字 N,我必须找到它所包含的所有质数

python - QListWidget添加后可以刷新吗?