MaiaVictor
MaiaVictor

Reputation: 53037

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.

Upvotes: 0

Views: 2213

Answers (5)

zweihander
zweihander

Reputation: 6305

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

Upvotes: 3

fkl
fkl

Reputation: 5535

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>

Upvotes: 1

vahancho
vahancho

Reputation: 21248

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

Upvotes: 1

Adri C.S.
Adri C.S.

Reputation: 3007

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.

Upvotes: 2

4pie0
4pie0

Reputation: 29744

try instead #include <QtGui/QApplication>

Upvotes: 3

Related Questions