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.