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
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值