Qt Creator's intellisense grays out #ifdef _DEBUG blocks even though Debug build is selected
Asked Answered
P

1

7

When I have in my code an #ifdef like this

#ifdef _DEBUG
    printf("This is a debug build");
#endif

Qt Creator grays out the printf even though it is a debug build, and when I run it "This is a debug build" gets printed.

It seems that the intellisense isn't seeing the preprocessor defines that will be passed to the compiler via the command line (i.e. -D_DEBUG) instead of being defined in the header/source files.

Producer answered 21/2, 2013 at 9:5 Comment(4)
You'll want to look at this answer https://mcmap.net/q/400689/-how-to-add-pre-processing-defs-macros-to-qt-creatorAleasealeatory
Thanks for the link. I don't have a config file in my project directory. Also how do I make it define _DEBUG for debug builds only, but not for release builds?Producer
Just create it. I don't know how to do it conditionally on the build type, though.Aleasealeatory
It needs to change automatically between Debug and Release builds, otherwise it wouldn't be a useful workaround. I would need to edit the .config file each time I change the Build mode. This probably is a bug/oversight in Qt Creator. The intellisense should see the same preprocessor defines that the compiler will see.Producer
A
12

The following works just fine in my *.pro file:

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

Switching the build type then also swaps the greyed-out area. I'm using Qt5, though.

Aleasealeatory answered 21/2, 2013 at 10:6 Comment(1)
Thanks for this little but very useful for me a Qt (Creator) newbie hint!Darill

© 2022 - 2024 — McMap. All rights reserved.