检测 qt 是否在运行时运行调试版本 [英] Detect if qt is running a debug build at runtime

查看:98
本文介绍了检测 qt 是否在运行时运行调试版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在运行时从 QObject 中检测它所链接的 Qt 是调试版本还是发布版本?

我知道 QT_NO_DEBUG 宏,但这在 Qt 构建时已解决.

示例 1 何时有用:我有一个插件充当崩溃处理程序,提供回溯.如果 qt 调试符号可用,我只希望这个插件尝试完成所有这些工作.

示例 2:我有一个用 pyqt 编写的命令行参数解析器.它想打印程序帮助.它如何从 pyqt 中找出 QApplication 是否支持 -sync 开关.如果我有关于构建类型的信息,我可以轻松地从程序理解的参数列表中添加或删除 -sync.

解决方案

如果您只想测试您正在运行的是应用程序的调试版本还是发布版本,请使用QT_DEBUG:

#ifdef QT_DEBUGqDebug() <<运行调试版本";#别的qDebug() <<运行发布版本";#万一

虽然这显然使用了预处理器,而不是在运行时检查.我对您为什么指定要在运行时进行此检查感到有些困惑,因为只能在编译之前决定是否使用调试功能进行构建...

我不知道 Qt4 中有任何简单的方法来检查您所链接的 Qt 库是否是在启用调试的情况下构建的(尽管我确实注意到在 Qt5 中添加了 QLibraryInfo::isDebugBuild()).

How can I detect from within a QObject at runtime whether or not the Qt it's linked against is a debug build or release build?

I know about the QT_NO_DEBUG macro, but that is resolved at build time for Qt.

Example 1 of when this would be useful: I have a plugin that acts as a crash handler, providing a backtrace. I only want this plugin to attempt to do all this work if the qt debug symbols are available.

Example 2: I have a command line argument parser written in pyqt. It wants to print program help. How does it find out if QApplication will support the -sync switch or not from within pyqt. If I had info on the build type, I could add or remove -sync easily from the list of arguments the program understands.

解决方案

If you just want to test whether you are running a debug or release build of your application, use QT_DEBUG:

#ifdef QT_DEBUG
  qDebug() << "Running a debug build";
#else
  qDebug() << "Running a release build";
#endif

Though this obviously uses the pre-processor, rather than checking at runtime. I'm a bit confused as to why you've specified that you want to make this check at runtime, seeing as the decision to build with or without debug capability can only be made prior to compiling...

I'm not aware of any easy way in Qt4 to check whether the Qt library you're linking against has been built with debugging enabled (though I did notice that this looks to be changing in Qt5 with the addition of QLibraryInfo::isDebugBuild()).

这篇关于检测 qt 是否在运行时运行调试版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆