Qt程序的调试版和发行版对应的宏定义QT_DEBUG和QT_NO_DEBUG

在Qt编程中,有时候需要针对Debug调试版和Release发行版做条件编译,做不同的处理,比如有时在Debug版中需要在控制台打印日志,在Release版中将日志写入到文件中。
Qt中提供了QT_DEBUG这个调试版宏,以及QT_NO_DEBUG这个发行版宏。
https://stackoverflow.com/中看到了很老的一篇文章:Does Qt offer a (guaranteed) debug definition?

Does Qt offer a (guaranteed) debug definition?

Does anyone know an officially supported way to include debug-build only code in Qt? For example:

#ifdef QT_DEBUG
// do something
#endif

Basically like Q_ASSERT but for more complex tests.

I can’t seem to find any documentation which says that the Qt framework guarantees to define a debug macro. If there isn’t, what would be a sensible unofficial way to implement this feature project wide?

Qt defines QT_NO_DEBUG for release builds. Otherwise QT_DEBUG is defined.

Of course you are free to specify any DEFINES in your .pro files and scope them for either debug or release.

An alternative is to write in your project file something like:

debug {
  DEFINES += MYPREFIX_DEBUG
}
release {
  DEFINES += MYPREFIX_RELEASE
}

Then you will not depend on the Qt internal definition.

For check debug mode:

#ifdef QT_DEBUG
    //Some codes
#endif

For check release mode:

#ifndef QT_DEBUG    //<== Please note... if not defined
    //Some codes
#endif

也就是说,Qt提供了针对Debug和Release模式的条件编译宏,分别对应QT_DEBUG和QT_NO_DEBUG
1、检查Debug模式,可以采用类似如下的代码:

#ifdef QT_DEBUG
    //Some codes
#endif

或者:

#ifndef QT_NO_DEBUG
// do something
#endif

2、检查Release模式,可以采用如下代码:

#ifndef QT_DEBUG    //<== Please note... if not defined
    //Some codes
#endif

或者:

#ifdef QT_NO_DEBUG
// do something
#endif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值