Previous Article: translating the qmake document (I) qmqke guide and overview
Original English document: http://qt-project.org/doc/qt-5/qmake-tutorial.html this tutorial teaches the basic knowledge of qmake. Other topics in this Manual contain more detailed information on qmke usage.
Start with simpleAssume that you have completed the basic implementation of the application and created the following file:
hello.cpp hello.h main.cpp
You can find these files in the directory examples/qmake/tutorial of QT distribution. Another thing you only need to know is that the installation program of this program is written in QT. First, use your favorite text editing tool to create Hello under the examples/qmake/tutorial directory. pro file, the first thing you need to do is to add some code to tell qmak which source files and header files are part of the project. First, add the source file to the project. To do this, you need to use the sources variable. You only need to use sources + = to start a new row and then write hello. cpp behind it. The effect after writing is as follows:
SOURCES += hello.cpp
We repeat every source file in the project until we end the process as follows:
SOURCES += hello.cppSOURCES += main.cpp
If you like to use the make-like syntax to list all files in one breath, you can use line breaks, just like below
SOURCES = hello.cpp main.cpp
All source files in the project have been listed. You must add the header files in the same way as the add source files except for the variables named headers.
After doing so, your project file should look like this:
HEADERS += hello.hSOURCES += hello.cppSOURCES += main.cpp
The target name is automatically set. Hello on the UNIX platform. If you want to use different names, you can set them in the project file:
TARGET = helloworld
The last step is to set the config variable. Because this is a QT application, we need to add QT to the config line, so that qmake will connect the relevant library to ensure that the MOC and UIC build lines can be included in the generated makefile. The completed project file should be the same as the following:
CONFIG += qt HEADERS += hello.h SOURCES += hello.cpp SOURCES += main.cpp
Now you can use qmake to generate makefile for your application. In the command line, write the following command line to your project directory:
qmake -o Makefile hello.pro
Then input make or nmake according to the compiler you use.
For Visual Studio users, qmake can also generate. DSP or. vcproj, for example:
qmake -tp vc hello.pro
Debuggable applications
The release version application does not contain any debug symbols or debug information. It is very useful to generate the debug version for the application during development. You can easily add debug to the config variable in the project file.
For example:
CONFIG += qt debug HEADERS += hello.h SOURCES += hello.cpp SOURCES += main.cpp
Use qmake to generate makefile as before. When you run an application in the debug environment, you will get useful information.
Add platform-specific source filesAfter writing the code for a few hours, you may start to write the specific platform for your application, and insist on writing code that is not supported by different platforms. Now you have two new files to be included in the project: hellowin. cpp and hellounix. cpp. When we want to put these two files into makefile, we cannot just add these files to the sources variable. Therefore, what we need to do is to use the corresponding scope to process the current platform running qmake. Add a simple scope to a file dependent on the Windows platform, as shown below:
win32 { SOURCES += hellowin.cpp }
Therefore, if qmake runs on the window platform, it will add hellowin. cpp to the source file list. If qmake runs on other platforms, it will be ignored. What is needed now is to create a Unix-specific scope.
After you finish this, your project looks like this:
CONFIG += qt debug HEADERS += hello.h SOURCES += hello.cpp SOURCES += main.cpp win32 { SOURCES += hellowin.cpp } unix { SOURCES += hellounix.cpp }
Just like a previous makefile generated using QT
If a file does not exist, stop qmake.
If a file does not exist, you may not want to generate the makefile. We can use the exists () method to check whether a file exists. With the error () method, we can stop running qmake. This is the same as the working method of the scope. This method is used to replace the scope condition. Check the main. cpp file as follows:
!exists( main.cpp ) { error( "No main.cpp file found" )}
"!" Is used to deny the test. In this way, if exists (main. cpp) is true, the file exists. If! Exists (main. cpp) is true, so this file does not exist.
CONFIG += debugHEADERS += hello.hSOURCES += hello.cppSOURCES += main.cppwin32 { SOURCES += hellowin.cpp}unix { SOURCES += hellounix.cpp}!exists( main.cpp ) { error( "No main.cpp file found" )}
Use qmake to generate makefile as before. If you temporarily rename main. cpp, you will see the error message and qmake will also stop running.
Multi-condition check
Suppose that you are using a Windows system. When you run a program, you want to see the qdebug () Statement output in the command line. To see the output, you must use the appropriate console settings to build your application. We can simply add the console to the config line. The makefile of the window platform contains this setting. However, if we only want to add the console to the config line when running on the window platform and debug has been added to the config line. This requires two nested scopes. Create a scope first, and then create another scope in it. Place the settings to be processed in the second scope, as shown in the following code:
win32 { debug { CONFIG += console }}
Nested scopes can be connected by colons. The final project file is as follows:
CONFIG += debugHEADERS += hello.hSOURCES += hello.cppSOURCES += main.cppwin32 { SOURCES += hellowin.cpp}unix { SOURCES += hellounix.cpp}!exists( main.cpp ) { error( "No main.cpp file found" )}win32:debug { CONFIG += console}
Now that you have completed the qmake tutorial, prepare a project file for your development project.
Translate the qmake document (2) getting started