在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