【发布时间】:2016-08-16 10:49:59
【问题描述】:
我尝试按照书中的示例使用 Qt 学习 C++。在包括 Qt 在内的第一个示例中,我遇到了编译错误:
$ g++ -Wall qtdemo.cpp
qtdemo.cpp:1:19: fatal error: QString: No such file or directory
compilation terminated.
如果我不包含任何 Qt 库,g++ 可以正常工作。
我尝试使用 Qt Creator 运行相同的程序,如果我执行“Qt Console 应用程序”,它可以工作,但如果我执行“Plain C++ 应用程序”则显示错误。
我试图在 g++ 命令、不同配置等之后给出QString 的路径。似乎 g++ 根本看不到 Qt。
我什至在 Ubuntu 16.04 Qt4 上已经存在的基础上安装了 Qt5。
当我检查我是否安装了 Qt 时,看起来一切都很好:
$ locate QString
/opt/Qt5.7.0/5.7/Src/qtbase/include/QtCore/QString
...
/opt/Qt5.7.0/5.7/gcc_64/include/QtCore/QString
...
/usr/include/qt4/QtCore/QString
...
$ qmake -v
QMake version 2.01a
Using Qt version 4.8.7 in /usr/lib/x86_64-linux-gnu
我尝试运行的代码:
#include <QString>
#include <QTextStream>
QTextStream cout(stdout);
QTextStream cin(stdin);
int main() {
QString s1("This "), s2("is s "), s3("string");
s1 += s2; //concatenation
QString s4 = s1+s3;
cout << s4 << endl;
cout << "The length of that string is " << s4.length() << endl;
cout << "Enter a sentence with withspace: " << endl;
s2 = cin.readLine();
cout << "Here is your sentence: \n" << s2 << endl;
cout << "The length of your sentnce is: " << s2.length() << endl;
return 0;
}
我不知道如何使用 g++ 使 Qt 应用程序在命令行中运行以及错误来自何处。
【问题讨论】:
-
/opt 下的东西(通常)不在通常的库中或包含链接器的文件搜索路径,因此您必须提供适当的编译器选项。但是,只需使用 qmake 或 qbs 来管理您的 Qt 项目,因为您不想手动做这些事情......