27

Coming from MFC, I treated qDebug() much like TRACE(), assuming that it is removed from Release builds by the preprocessor (in MFC it's done using #define TRACE 1 ? (void*) 0 : AfxTrace).

To my surprise, however, qDebug() is executed in Release builds as well. How do I change this? And also, why is this so, what was the reasoning of the developers of Qt behind this decision?

sashoalm
  • 75,001
  • 122
  • 434
  • 781
  • useful links: http://qt-project.org/forums/viewthread/25512 and http://supportforums.blackberry.com/t5/Native-Development/Dealing-with-qDebug-in-release-candidate/td-p/2078713 – x29a Feb 20 '14 at 18:31

3 Answers3

30

qDebug is also preprocessor-controlled, but it has its own special macro, QT_NO_DEBUG_OUTPUT. If you add that to your Release build defines, it will be removed.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • So why is it not set by default? There must be a reason they decided on that. – sashoalm Nov 21 '12 at 13:48
  • @sashoalm My guess would be that [people aren't always careful to distinguish stdin from stdout](http://stackoverflow.com/a/3886128/1858225) (see the comments on that answer, as well as my answer below it; the original answer consisted only of the first code block), and it would be bad practice to cause debug builds to behave substantially differently from release builds by default. – Kyle Strand Mar 24 '15 at 21:01
17

QDebug is "output stream for debugging information". It has it default behaviour witch is printing to stdout/stderr depending on message type. You can customize qDebug() behaviour easily by installing own message handler. For example you can test at runtime (not compile time) if you want to print debugs. Take a look at this code sample:

#include <QDebug>

void noMessageOutput(QtMsgType type, const char *msg)
{
     Q_UNUSED(type);
     Q_UNUSED(msg);
}

int main(int argc, char * argv[])
{
    QApplication app(argc, argv);

    if ( ! app.arguments().contains(QLatin1String("--with-debug") ) {
        qInstallMsgHandler(noMessageOutput);
    }
}

It will hide whole qDebug output if there is no parameter specified at runtime. You get more control than just "show debug/don't show debug"

Also you can completly disable QDebug with QT_NO_DEBUG_OUTPUT define if you're concerned about performance lost with qDebug present within code.

Kamil Klimek
  • 12,884
  • 2
  • 43
  • 58
4

Use this to suppress messages in release mode but allow them in debug mode:

CONFIG(release, debug|release):DEFINES += QT_NO_DEBUG_OUTPUT

If you use only DEFINES += QT_NO_DEBUG_OUTPUT without the CONFIG(...) part you will disable them in both modes, which is usually not desirable.

  • My config: CONFIG += qt warn_off release CONFIG(release, debug|release):DEFINES += QT_NO_DEBUG_OUTPUT I don't know why both mode is disable debug output – kien bui Oct 17 '18 at 06:15