[英]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.