28

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.

troy.unrau
  • 1,142
  • 2
  • 12
  • 26
  • Do you want to know if the Qt libraries are debug or release or do you want to know if your application is compiled in debug or release? – Luca Carlon Jul 30 '12 at 08:22

2 Answers2

55

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()).

Teivaz
  • 5,462
  • 4
  • 37
  • 75
sam-w
  • 7,478
  • 1
  • 47
  • 77
  • Added clarification to question. – troy.unrau Jul 30 '12 at 18:58
  • 2
    Accepting your answer, not for the macros, but for the QLibraryInfo tidbit. It appears that many of the questions I've been asking are solved with Qt5, including this one. I will accede that there is no proper solution for Qt4. – troy.unrau Aug 08 '12 at 00:56
2

Both hints in accepted answer are true. There is one side effect with Qt5 on macOS. By Default, frameworks use a release version of it's library and the result of this method will be always 'false' value.

Ľubomír Carik
  • 387
  • 5
  • 11