简体   繁体   中英

Can't get a Qt Hello World to work

I've compiled Qt on OSX Lion using the instructions provided at this official guide . I've then tried compiling the following Hello World with gcc hello_world.cpp -o hello_world

#include <QApplication>

int main(int argc, char **argv)
{
    QApplication app (argc, argv);
    return app.exec();
}

I've got the following error:

hello_world.cpp:1:10: fatal error: 'QApplication' file not found
#include <QApplication>
         ^
1 error generated.

请尝试#include <QtGui/QApplication>

Use -I option of gcc to give additional include locations.

gcc hello_world.cpp -I/path-to-qt/include -o hello_world

If you use it like that, you have to use your includes like this:

#include <QtGui/QApplication>

if you want your includes to be shorter like #include <QApplication> , you can give multiple include folders like this:

gcc hello_world.cpp -I/path-to-qt/include/QtCore -I/path-to-qt/include/QtGui -o hello_world

But that is not all. You also have to give library directories and which libraries to link to, which is done like this:

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

It is also better to use g++, since you are using 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

Try with g++ -I<path_to_include_directory> -L<path_to_library_dir> -lQtCore .

For example, in my Debian I would do: g++ -I/usr/local/include/Qt4 -L/usr/local/lib -lQtCore -lQtGui whatever.cpp

EDIT: Thanks to @erelender to point out that QApplication is in the QtGui library and that it depends on QtCore .

Not sure about the path in mac, but on Linux the class QApplication is defined at following location (qt4)

/usr/include/qt4/QtGui/qwindowdefs.h

Do you have something similar on Mac?

If you are building from command line, including a header file with gcc can be done with following switch

-I<path to .h file>

What if you try to add additional include path for gcc with using -I flags? Something like:

gcc -I/usr/local/Qt-5.1.1/include hello_world.cpp -o hello_world

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM