[Qt-interest] qDebug with #ifdef macros ?

Rohan McGovern rohan.mcgovern at nokia.com
Sun Oct 3 00:43:42 CEST 2010


Aaron Lewis said:
> 
>     #ifdef MYDBG
>     #define DBG qDebug()
>     #else
>     #define DBG
>     #endif
> 
>     If macro MYDBG is defined , qDebug() will works as usual , else
> it's an empty function.
> 
>     and if i call it like this :
>    
>     DBG << "Bonjour Aaron";
> 
>     It won't even compile , anyone got some ideas to help ? qDebug(
> fmt .. ) isn't really cool somehow ..
> 

It doesn't compile because (as already pointed out) the code expands to

  << "Bonjour Aaron";

You can try something like this:

    #ifdef MYDBG
    #define DBG qDebug()
    #else
    #define DBG if (0) qDebug()
    #endif

Then in the non-debug case, the code expands to:

    if (0) qDebug() << "Bonjour Aaron";

Which is valid, but does nothing at runtime, and generally will not
cause any machine code to be generated at compile time.
-- 
Rohan McGovern
QA Engineer
Qt Development Frameworks, Nokia



More information about the Qt-interest-old mailing list