![]() |
Home · Examples |
[qmake Manual][Next: qmake Common Projects]
This tutorial teaches you how to use qmake. We recommend that you read the qmake user guide after completing this tutorial. We'll add the source files to the project file first. To do this you need to use the SOURCES variable. Just start a new line with SOURCES += and put hello.cpp after it. You should have something like this: Starting off Simple
Let's assume that you have just finished a basic implementation of your application, and you have created the following files:
You will find these files in the examples/qmake/tutorial directory of the Qt distribution. The only other thing you know about the setup of the application is that it's written in Qt. First, using your favorite plain text editor, create a file called hello.pro in examples/qmake/tutorial. The first thing you need to do is add the lines that tell qmake about the source and header files that are part of your development project.
SOURCES += hello.cpp
We repeat this for each source file in the project, until we end up with the following:
SOURCES += hello.cpp
SOURCES += main.cpp
If you prefer to use a Make-like syntax, with all the files listed in one go you can use the newline escaping like this:
SOURCES = hello.cpp \
main.cpp
Now that the source files are listed in the project file, the header files must be added. These are added in exactly the same way as source files, except that the variable name we use is HEADERS.
Once you have done this, your project file should look something like this:
HEADERS += hello.h SOURCES += hello.cpp SOURCES += main.cppThe target name is set automatically; it is the same as the project file, but with the suffix appropriate to the platform. For example, if the project file is called hello.pro, the target will be hello.exe on Windows and hello on Unix. If you want to use a different name you can set it in the project file:
TARGET = helloworldThe final step is to set the CONFIG variable. Since this is a Qt application, we need to put qt on the CONFIG line so that qmake will add the relevant libraries to be linked against and ensure that build lines for moc and uic are included in the generated Makefile.
The finished project file should look like this:
CONFIG += qt HEADERS += hello.h SOURCES += hello.cpp SOURCES += main.cppYou can now use qmake to generate a Makefile for your application. On the command line, in your project's directory, type the following:
qmake -o Makefile hello.proThen type make or nmake depending on the compiler you use.
For Visual Studio users, qmake can also generate .dsp or .vcproj files, for example:
qmake -tp vc -o hello.dsp hello.pro
For example:
CONFIG += qt debug HEADERS += hello.h SOURCES += hello.cpp SOURCES += main.cppUse qmake as before to generate a Makefile and you will be able to obtain useful information about your application when running it in a debugging environment.
A simple scope that will add in the platform-dependent file for Windows looks like this:
win32 { SOURCES += hellowin.cpp }So if qmake is run on Windows, it will add hellowin.cpp to the list of source files. If qmake is run on any other platform, it will simply ignore it. Now all that is left to be done is to create a scope for the Unix-specific file.
When you have done that, your project file should now look something like this:
CONFIG += qt debug HEADERS += hello.h SOURCES += hello.cpp SOURCES += main.cpp win32 { SOURCES += hellowin.cpp } unix { SOURCES += hellounix.cpp }Use qmake as before to generate a Makefile.
!exists( main.cpp ) { error( "No main.cpp file found" ) }The ! symbol is used to negate the test; i.e. exists( main.cpp ) is true if the file exists, and !exists( main.cpp ) is true if the file doesn't exist.
CONFIG += qt debug HEADERS += hello.h SOURCES += hello.cpp SOURCES += main.cpp win32 { SOURCES += hellowin.cpp } unix { SOURCES += hellounix.cpp } !exists( main.cpp ) { error( "No main.cpp file found" ) }Use qmake as before to generate a makefile. If you rename main.cpp temporarily, you will see the message and qmake will stop processing.
win32 { debug { CONFIG += console } }Nested scopes can be joined together using colons, so the final project file looks like this:
CONFIG += qt debug HEADERS += hello.h SOURCES += hello.cpp SOURCES += main.cpp win32 { SOURCES += hellowin.cpp } unix { SOURCES += hellounix.cpp } !exists( main.cpp ) { error( "No main.cpp file found" ) } win32:debug { CONFIG += console }That's it! You have now completed the tutorial for qmake, and are ready to write project files for your development projects.
Copyright © 2008 Trolltech | Trademarks | Qt Jambi 4.4.2_01 |