#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 );
hello.show();
return a.exec();
}
Save the file and exit your editor. Again, read through http://doc.trolltech.com/t1.html 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.
$> 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 ``libqt.so''. 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).
$> 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.
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 hello.pro file with the following content:
HEADERS = SOURCES = hello.cpp TARGET = hello
Then let tmake create the Makefile and finally let make build the program for you:
$> tmake hello.pro -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.