qmake Tutorial

This tutorial teaches you how to use qmake . We recommend that you read the qmake user guide after completing this tutorial.

Starting off Simple

假定您已完成应用程序的基本实现,且有创建以下文件:

  • hello.cpp
  • hello.h
  • main.cpp

将找到这些文件在 examples/qmake/tutorial Qt 分发目录。您对应用程序设置唯一知道的事情是,它是以 Qt 编写的。首先,使用喜爱的纯文本编辑器,创建文件称为 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 变量。仅仅开始新行采用 SOURCES += 并将 hello.cpp 放于其后。应该有的内容像这样:

SOURCES += hello.cpp
					

对各工程源文件重复这,直到得到以下结束:

SOURCES += hello.cpp
SOURCES += main.cpp
					

若首选使用像 make 句法,可以像这样使用换行转义将所有文件一起列出:

SOURCES = hello.cpp \
          main.cpp
					

现在源文件已列表于工程文件中,必须添加头文件。这些文件的添加方式与源文件准确相同,除了使用的变量名是 HEADERS .

一旦完成这,工程文件看起来应该像这样:

HEADERS += hello.h
SOURCES += hello.cpp
SOURCES += main.cpp
					

The 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 ,目标将是 hello.exe 在 Windows 和 hello 在 Unix。若想要使用不同名称,可以在工程文件中设置:

TARGET = helloworld
					

The final step is to set the CONFIG variable. Since this is a Qt application, we need to put qt 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.

完成后的工程文件应该看起来像这样:

CONFIG += qt
HEADERS += hello.h
SOURCES += hello.cpp
SOURCES += main.cpp
					

现在可以使用 qmake to generate a Makefile for your application. On the command line, in your project's directory, type the following:

qmake -o Makefile hello.pro
					

然后键入 make or nmake 取决于所用的编译器。

For Visual Studio users, qmake can also generate .dsp or .vcproj files, for example:

qmake -tp vc hello.pro
					
					

使应用程序可调试

The release version of an application doesn't contain any debugging symbols or other debugging information. During development it is useful to produce a debugging version of the application that has the relevant information. This is easily achieved by adding debug CONFIG 变量在工程文件。

例如:

CONFIG += qt debug
HEADERS += hello.h
SOURCES += hello.cpp
SOURCES += main.cpp
					

使用 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.

添加特定平台源文件

经几小时编码,您可能已开始应用程序的特定平台部分,并决定单独从属平台代码。因此,现在有 2 个新文件要包括到工程文件中: hellowin.cpp and hellounix.cpp . We can't just add these to the SOURCES variable since this will put both files in the Makefile. So, what we need to do here is to use a scope which will be processed depending on which platform qmake is run on.

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
}
					

使用 qmake as before to generate a Makefile.

Stopping qmake If a File Doesn't Exist

You may not want to create a Makefile if a certain file doesn't exist. We can check if a file exists by using the exists() function. We can stop qmake from processing by using the error() function. This works in the same way as scopes do. Simply replace the scope condition with the function. A check for a main.cpp file looks like this:

!exists( main.cpp ) {
    error( "No main.cpp file found" )
}
					

The ! symbol is used to negate the test; i.e. exists( main.cpp ) 为 true 若文件存在,而 !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" )
}
					

使用 qmake as before to generate a makefile. If you rename main.cpp temporarily, you will see the message and qmake will stop processing.

校验多个条件

假设使用 Windows,且想要能够看到语句输出采用 qDebug () when you run your application on the command line. Unless you build your application with the appropriate console setting, you won't see the output. We can easily put console CONFIG line so that on Windows the makefile will have this setting. However, let's say that we only want to add the CONFIG line if we are running on Windows and debug 已在 CONFIG line. This requires using two nested scopes; just create one scope, then create the other inside it. Put the settings to be processed inside the last scope, like this:

win32 {
    debug {
        CONFIG += console
    }
}
					

可以使用冒号将嵌套作用域拼接在一起,因此,最终工程文件看起来像这样:

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.