[英]Can't get a Qt Hello World to work
我已经使用此官方指南提供的说明在OSX Lion上编译了Qt。 然后,我尝试使用gcc hello_world.cpp -o hello_world
编译以下Hello World
#include <QApplication>
int main(int argc, char **argv)
{
QApplication app (argc, argv);
return app.exec();
}
我遇到以下错误:
hello_world.cpp:1:10: fatal error: 'QApplication' file not found
#include <QApplication>
^
1 error generated.
请尝试#include <QtGui/QApplication>
使用gcc的-I选项提供其他包含位置。
gcc hello_world.cpp -I/path-to-qt/include -o hello_world
如果这样使用它,则必须这样使用include:
#include <QtGui/QApplication>
如果您希望包含的内容像#include <QApplication>
一样短,则可以提供多个包含文件夹,如下所示:
gcc hello_world.cpp -I/path-to-qt/include/QtCore -I/path-to-qt/include/QtGui -o hello_world
但这还不是全部。 您还必须提供库目录以及要链接到的库,这是这样完成的:
gcc hello_world.cpp -I/path-to-qt/include/QtCore -I/path-to-qt/include/QtGui -o hello_world -L/path-to-qt/lib -lQtCore -lQtGui
最好使用g ++,因为您使用的是C ++。
g++ hello_world.cpp -I/path-to-qt/include/QtCore -I/path-to-qt/include/QtGui -o hello_world -L/path-to-qt/lib -lQtCore -lQtGui
尝试使用g++ -I<path_to_include_directory> -L<path_to_library_dir> -lQtCore
。
例如,在我的Debian中,我会这样做: g++ -I/usr/local/include/Qt4 -L/usr/local/lib -lQtCore -lQtGui whatever.cpp
编辑:感谢@erelender指出QApplication
在QtGui
库中,并且它依赖于QtCore
。
不确定mac中的路径,但在Linux上,在以下位置(qt4)定义了QApplication类
/usr/include/qt4/QtGui/qwindowdefs.h
您在Mac上有类似的东西吗?
如果您是从命令行构建的,则可以使用以下开关完成包含带有gcc的头文件的操作
-I<path to .h file>
如果您尝试使用-I标志为gcc添加其他包含路径,该怎么办? 就像是:
gcc -I/usr/local/Qt-5.1.1/include hello_world.cpp -o hello_world
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.