qmake 语言

Many qmake project files simply describe the sources and header files used by the project, using a list of name = value and name += value definitions. qmake also provides other operators, functions, and scopes that can be used to process the information supplied in variable declarations. These advanced features allow Makefiles to be generated for multiple platforms from a single project file.

运算符

在很多工程文件中,赋值 ( = ) 和追加 ( += ) operators can be used to include all the information about a project. The typical pattern of use is to assign a list of values to a variable, and append more values depending on the result of various tests. Since qmake defines certain variables using default values, it is sometimes necessary to use the removal ( -= ) operator to filter out values that are not required. The following sections describe how to use operators to manipulate the contents of variables.

赋值值

The = 运算符将值赋值给变量:

TARGET = myapp
					

The above line sets the TARGET 变量到 myapp . This will overwrite any values previously set for TARGET with myapp .

追加值

The += operator appends a new value to the list of values in a variable:

DEFINES += USE_MY_STUFF
					

The above line appends USE_MY_STUFF to the list of pre-processor defines to be put in the generated Makefile.

移除值

The -= operator removes a value from the list of values in a variable:

DEFINES -= USE_MY_STUFF
					

The above line removes USE_MY_STUFF from the list of pre-processor defines to be put in the generated Makefile.

添加唯一值

The *= operator adds a value to the list of values in a variable, but only if it is not already present. This prevents values from being included many times in a variable. For example:

DEFINES *= USE_MY_STUFF
					

In the above line, USE_MY_STUFF will only be added to the list of pre-processor defines if it is not already defined. Note that the unique() function can also be used to ensure that a variable only contains one instance of each value.

替换值

The ~= operator replaces any values that match a regular expression with the specified value:

DEFINES ~= s/QT_[DT].+/QT
					

In the above line, any values in the list that start with QT_D or QT_T are replaced with QT .

变量扩展

The $$ operator is used to extract the contents of a variable, and can be used to pass values between variables or supply them to functions:

EVERYTHING = $$SOURCES $$HEADERS
message("The project contains the following files:")
message($$EVERYTHING)
					

Variables can be used to store the contents of environment variables. These can be evaluated at the time when qmake is run, or included in the generated Makefile for evaluation when the project is built.

To obtain the contents of an environment value when qmake is run, use the $$(...) 运算符:

DESTDIR = $$(PWD)
message(The project will be installed in $$DESTDIR)
					

In the above assignment, the value of the PWD environment variable is read when the project file is processed.

To obtain the contents of an environment value at the time when the generated Makefile is processed, use the $(...) 运算符:

DESTDIR = $$(PWD)
message(The project will be installed in $$DESTDIR)
DESTDIR = $(PWD)
message(The project will be installed in the value of PWD)
message(when the Makefile is processed.)
					

In the above assignment, the value of PWD is read immediately when the project file is processed, but $(PWD) is assigned to DESTDIR in the generated Makefile. This makes the build process more flexible as long as the environment variable is set correctly when the Makefile is processed.

访问 qmake 特性

特殊 $$[...] operator can be used to access qmake properties:

message(Qt version: $$[QT_VERSION])
message(Qt is installed in $$[QT_INSTALL_PREFIX])
message(Qt resources can be found in the following locations:)
message(Documentation: $$[QT_INSTALL_DOCS])
message(Header files: $$[QT_INSTALL_HEADERS])
message(Libraries: $$[QT_INSTALL_LIBS])
message(Binary files (executables): $$[QT_INSTALL_BINS])
message(Plugins: $$[QT_INSTALL_PLUGINS])
message(Data files: $$[QT_INSTALL_DATA])
message(Translation files: $$[QT_INSTALL_TRANSLATIONS])
message(Settings: $$[QT_INSTALL_CONFIGURATION])
message(Examples: $$[QT_INSTALL_EXAMPLES])
					

更多信息,见 配置 qmake .

The properties accessible with this operator are typically used to enable third party plugins and components to be integrated in Qt. For example, a Qt Designer plugin can be installed alongside Qt Designer 's built-in plugins if the following declaration is made in its project file:

target.path = $$[QT_INSTALL_PLUGINS]/designer
INSTALLS += target
					

作用域

Scopes are similar to if statements in procedural programming languages. If a certain condition is true, the declarations inside the scope are processed.

作用域句法

Scopes consist of a condition followed by an opening brace on the same line, a sequence of commands and definitions, and a closing brace on a new line:

<condition> {
    <command or definition>
    ...
}
					

The opening brace must be written on the same line as the condition . Scopes may be concatenated to include more than one condition, as described in the following sections.

作用域和条件

A scope is written as a condition followed by a series of declarations contained within a pair of braces. For example:

win32 {
    SOURCES += paintwidget_win.cpp
}
					

The above code will add the paintwidget_win.cpp file to the sources listed in the generated Makefile when building for a Windows platform. When building for other platforms, the define will be ignored.

The conditions used in a given scope can also be negated to provide an alternative set of declarations that will be processed only if the original condition is false. For example, to process something when building for all platforms except Windows, negate the scope like this:

!win32 {
    SOURCES -= paintwidget_win.cpp
}
					

Scopes can be nested to combine more than one condition. For instance, to include a particular file for a certain platform only if debugging is enabled, write the following:

macx {
    CONFIG(debug, debug|release) {
        HEADERS += debugging.h
    }
}
					

To save writing many nested scopes, you can nest scopes using the : operator. The nested scopes in the above example can be rewritten in the following way:

macx:CONFIG(debug, debug|release) {
    HEADERS += debugging.h
}
					

You may also use the : operator to perform single line conditional assignments. For example:

win32:DEFINES += USE_MY_STUFF
					

The above line adds USE_MY_STUFF DEFINES variable only when building for the Windows platform. Generally, the : operator behaves like a logical AND operator, joining together a number of conditions, and requiring all of them to be true.

There is also the | operator to act like a logical OR operator, joining together a number of conditions, and requiring only one of them to be true.

win32|macx {
    HEADERS += debugging.h
}
					

You can also provide alternative declarations to those within a scope by using an else scope. Each else scope is processed if the conditions for the preceding scopes are false. This allows you to write complex tests when combined with other scopes (separated by the : operator as above). For example:

win32:xml {
    message(Building for Windows)
    SOURCES += xmlhandler_win.cpp
} else:xml {
    SOURCES += xmlhandler.cpp
} else {
    message("Unknown configuration")
}
					

配置和作用域

The values stored in the CONFIG variable are treated specially by qmake. Each of the possible values can be used as the condition for a scope. For example, the list of values held by CONFIG can be extended with the opengl 值:

CONFIG += opengl
					

As a result of this operation, any scopes that test for opengl will be processed. We can use this feature to give the final executable an appropriate name:

opengl {
    TARGET = application-gl
} else {
    TARGET = application
}
					

This feature makes it easy to change the configuration for a project without losing all the custom settings that might be needed for a specific configuration. In the above code, the declarations in the first scope are processed, and the final executable will be called application-gl . However, if opengl is not specified, the declarations in the second scope are processed instead, and the final executable will be called application .

Since it is possible to put your own values on the CONFIG line, this provides you with a convenient way to customize project files and fine-tune the generated Makefiles.

平台作用域值

除了 win32 , macx ,和 unix values used in many scope conditions, various other built-in platform and compiler-specific values can be tested with scopes. These are based on platform specifications provided in Qt's mkspecs directory. For example, the following lines from a project file show the current specification in use and test for the linux-g++ specification:

message($$QMAKESPEC)
linux-g++ {
    message(Linux)
}
					

You can test for any other platform-compiler combination as long as a specification exists for it in the mkspecs 目录。

变量

Many of the variables used in project files are special variables that qmake uses when generating Makefiles, such as DEFINES , SOURCES ,和 HEADERS . In addition, you can create variables for your own use. qmake creates new variables with a given name when it encounters an assignment to that name. For example:

MY_VARIABLE = value
					

There are no restricitions on what you do to your own variables, as qmake will ignore them unless it needs to evaluate them when processing a scope.

You can also assign the value of a current variable to another variable by prefixing $$ to the variable name. For example:

MY_DEFINES = $$DEFINES
					

Now the MY_DEFINES variable contains what is in the DEFINES variable at this point in the project file. This is also equivalent to:

MY_DEFINES = $${DEFINES}
					

The second notation allows you to append the contents of the variable to another value without separating the two with a space. For example, the following will ensure that the final executable will be given a name that includes the project template being used:

TARGET = myproject_$${TEMPLATE}
					

替换函数

qmake provides a selection of built-in functions to allow the contents of variables to be processed. These functions process the arguments supplied to them and return a value, or list of values, as a result. To assign a result to a variable, use the $$ operator with this type of function as you would to assign contents of one variable to another:

HEADERS = model.h
HEADERS += $$OTHER_HEADERS
HEADERS = $$unique(HEADERS)
					

This type of function should be used on the right-hand side of assignments (that is, as an operand).

You can define your own functions for processing the contents of variables as follows:

defineReplace(functionName){
    #function code
}
					

The following example function takes a variable name as its only argument, extracts a list of values from the variable with the eval() built-in function, and compiles a list of files:

defineReplace(headersAndSources) {
    variable = $$1
    names = $$eval($$variable)
    headers =
    sources =
    for(name, names) {
        header = $${name}.h
        exists($$header) {
            headers += $$header
        }
        source = $${name}.cpp
        exists($$source) {
            sources += $$source
        }
    }
    return($$headers $$sources)
}
					

测试函数

qmake provides built-in functions that can be used as conditions when writing scopes. These functions do not return a value, but instead indicate success or failure :

count(options, 2) {
    message(Both release and debug specified.)
}
					

This type of function should be used in conditional expressions only.

It is possible to define your own functions to provide conditions for scopes. The following example tests whether each file in a list exists and returns true if they all exist, or false if not:

defineTest(allFiles) {
    files = $$ARGS
    for(file, files) {
        !exists($$file) {
            return(false)
        }
    }
    return(true)
}