前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Qt程序的调试版和发行版对应的宏定义QT_DEBUG和QT_NO_DEBUG

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

作者头像
ccf19881030
发布2021-10-22 10:46:59
2.7K0
发布2021-10-22 10:46:59
举报
文章被收录于专栏:ccf19881030的博客ccf19881030的博客

在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:

代码语言:javascript
复制
#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:

代码语言:javascript
复制
debug {
  DEFINES += MYPREFIX_DEBUG
}
release {
  DEFINES += MYPREFIX_RELEASE
}

Then you will not depend on the Qt internal definition.

For check debug mode:

代码语言:javascript
复制
#ifdef QT_DEBUG
    //Some codes
#endif

For check release mode:

代码语言:javascript
复制
#ifndef QT_DEBUG    //<== Please note... if not defined
    //Some codes
#endif

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

代码语言:javascript
复制
#ifdef QT_DEBUG
    //Some codes
#endif

或者:

代码语言:javascript
复制
#ifndef QT_NO_DEBUG
// do something
#endif

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

代码语言:javascript
复制
#ifndef QT_DEBUG    //<== Please note... if not defined
    //Some codes
#endif

或者:

代码语言:javascript
复制
#ifdef QT_NO_DEBUG
// do something
#endif
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-10-20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Does Qt offer a (guaranteed) debug definition?
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档