qt pro文件的幾個常用知識點

目錄

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狀態,則進入第一個花括號內執行;否則進入下一個花括號。

上面的寫法是否囉嗦?按照stackoverflow https://stackoverflow.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
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章