next up previous contents
Next: A (slightly) more complex Up: Compiling Your First Qt Previous: Introduction   Contents


Hello, World

Let's assume we wish to follow the tutorial as described on Using your favorite editor, create a file called "hello.cpp" and enter the following code:

#include <qapplication.h>                                                                                                   
#include <qpushbutton.h>                                                                                                    

int main( int argc, char **argv )
    QApplication a( argc, argv );
    QPushButton hello( "Hello world!", 0 );
    hello.resize( 100, 30 );
    a.setMainWidget( &hello );;
    return a.exec();

Save the file and exit your editor. Again, read through for a detailed explanation of the code. The sentence "You should now try to compile and run this program." is where this document attempts to fill the gap.

Compiling by hand

In one step

Assuming you have successfully and correctly installed the Qt-libraries and headers and set the appropriate environment variables accordingly, you should now be able to compile the application as follows.

    $> g++ -Wall -I$QTDIR/include -L$QTDIR/lib hello.cpp -o hello -lqt

Ok, let's explain what we are doing here in detail, what does all this mean? First off, g++, is of course the compiler. We tell it to warn us about everything that looks even remotely suspicious by passing the -Wall flag. Next, we tell the compiler where to look for the header files. As mentioned above, the environment variable $QTDIR must be set as suggested by the Qt Installation Documentation.

Suppose you have installed Qt in /usr/local/qt, then you could also use -I/usr/local/qt/include instead of -I$QTDIR/include. Note that there is no space between the -I and the $QTDIR!

Since this is a simple application, we can compile and link the application in one step, so the next flag tells the compiler where to search for a library (in addition to the standard directories): -L$QTDIR/lib. Again, note that there is no space between the flag and the directory!

Next, we pass the input file to the compiler (hello.cpp) and specify that the generated executable should be named ``hello'' (-o hello), as otherwise the compiler would produce a file called ``a.out''.

And finally, The next flag tells the compiler that we wish to link the application to a library which is called ``''. When linking, the ''lib`` and the file extension are left out, so that we end up with -lqt. Note that the library parameters are position sensitive, and that it therefore is a good idea to place the libraries at the end of the instructions.

For a more detailed documentation of the various compiler flags and an overview of the other flags you might want to pass to g++, refer to gcc(1).

In two steps

While the above example will work for a simple application that consists of only one file, you will of course encounter larger projects that require several source-files that need to be compiled and whose object-files then need to be linked together with the shared libraries. It is therefore a good idea to get used to separating the two steps (compiling and linking):

    $> g++ -Wall -I$QTDIR/include -c hello.cpp
    $> g++ -Wall -L$QTDIR/lib hello.o -lqt -o hello

The only difference here is that in the first step, we compile without linking, but instead we pass the -c flag to the compiler. This flag instructs the compiler to NOT attempt to link. Per default, the compiler will create an object-file with the ``.o'' ending.

In the second step, we take all object files that were compiled in the first step (one, in this case) and link them together with the appropriate libraries (-lqt, in this case) to form the executable ``hello''.

Again, refer to gcc(1) for details.

Using tmake

As you develop more and more complex programs, you will find the need to split the project over several files. Now you could, of course, always type in the same commands over and over again for all your source-files, but naturally, this becomes tedious very soon. This is where make comes into play. (If you don't know what make is, please read through make(1) and the documentation given in the ``References'' section of this document.) Creating proper Makefiles is not for the faint of heart, which is why Trolltech developed tmake, a little tool that will take over this task for you.

Once you installed tmake, please read through the documentation - you will find that is is incredibly easy to manage projects with this tool. For our little ``Hello, World'' example, we would need to create a file with the following content:

SOURCES    = hello.cpp
TARGET     = hello

Then let tmake create the Makefile and finally let make build the program for you:

    $> tmake -o Makefile
    $> make

Et voilà - ``hello'' has been built, much in the same way as we did by hand!

For additional information on the flags used by and on tmake in general, please refer to the tmake documentation.

next up previous contents
Next: A (slightly) more complex Up: Compiling Your First Qt Previous: Introduction   Contents
Jan Schaumann 2001-12-26