欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

qt pro文件的几个常用知识点

程序员文章站 2024-02-19 12:14:16
...

目录

CONFIG

DEFINES

$$

DESTDIR

MOC_DIR

OBJECTS_DIR

contains(variablename, value)


CONFIG

在pro文件里,CONFIG即可以作为一个变量来使用,也可以作为一个函数来使用(https://doc.qt.io/qt-5/qmake-test-function-reference.html#config-config)。

CONFIG(config)

This function can be used to test for variables placed into the CONFIG variable. This is the same as scopes, but has the added advantage that a second parameter can be passed to test for the active config. As the order of values is important in CONFIG variables (that is, the last one set will be considered the active config for mutually exclusive values) a second parameter can be used to specify a set of values to consider. For example:

CONFIG = debug
CONFIG += release
CONFIG(release, debug|release):message(Release build!) #will print
CONFIG(debug, debug|release):message(Debug build!) #no print

Because release is considered the active setting (for feature parsing) it will be the CONFIG used to generate the build file. In the common case a second parameter is not needed, but for specific mutual exclusive tests it is invaluable.

config作为函数的一个常见的用法如下:

CONFIG(debug, debug|release) {
    unix: TARGET = $$join(TARGET,,,_debug)
    else: TARGET = $$join(TARGET,,,d)
}
else
{
.....
}

CONFIG(debug, debug|release)相当于一个if 判断。假如CONFIG处于debug状态,则进入第一个花括号内执行;否则进入下一个花括号。

上面的写法是否啰嗦?按照* https://*.com/questions/1130106/linking-with-a-debug-release-lib-with-qmake-qt-creator的说法,上面的写法可以避免一些粗心引起的错误:

The normal

debug:LIBS += ...
else:LIBS += ...

solution breaks when users naively use CONFIG += debug or CONFIG += release to switch between debug and release builds (and they do; no-one remembers to say CONFIG -= release release_and_debug before CONFIG += debug :).

This is the canonical way to scope on debug:

CONFIG( debug, debug|release ) {
    # debug
    QMAKE_LIBDIR += "path/to/debug/lib"
} else {
    # release
    QMAKE_LIBDIR += "path/to/release/lib"
}

DEFINES

在C++的代码中,经常出现类似定义:

#define ______DEFINE_____ .....

pro文件中提供了DEFINES变量。只要在pro文件里定义了DEFINES,就不需要在c++代码里额外定义宏。

$$

根据QT官方文档,

The contents of a variable can be read by prepending the variable name with $$. This can be used to assign the contents of one variable to another

TEMP_SOURCES = $$SOURCES

在变量前面加$$,可以将其取值读取出来。

DESTDIR

The directory in which the executable or binary file will be placed.编译的结果(exe文件或dll文件等)放置的路径。

MOC_DIR

Specifies the directory where all intermediate moc files should be placed."moc"文件放置的路径。

OBJECTS_DIR

 Specifies the directory where all intermediate objects should be placed.中间结果.obj文件的放置路径。

contains(variablename, value)

Succeeds if the variable variablename contains the value value; otherwise fails. It is possible to specify a regular expression for parameter value.

You can check the return value of this function using a scope.

For example:

contains( drivers, network ) {
    # drivers contains 'network'
    message( "Configuring for network build..." )
    HEADERS += network.h
    SOURCES += network.cpp
}
相关标签: qt