Why does qDebug work in Release builds?

谁说我不能喝 提交于 2019-11-30 03:12:28

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.

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.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!