Eclipse CDT Does not track macro defines correctly
Asked Answered
T

5

13

In the build settings I have it define DEBUG when in the debug build configuration, so that I may make my code do separate things depending on which type of build it is.

However in Eclipse it darkens out the parts of the code which are excluded by the preprocessor, and this doesn't keep up when I change the setting (whether I'm currently building debug or release). So, I have a bunch of code which is perpetually darkened out, and the Eclipse indexer and other helpful features don't work inside of those areas.

How to fix? Has anyone else encountered this?

Update: Still having this issue. I eventually abandoned my unit-test build configs and simply put that functionality into a command-line switch. Just so that I could get indexing to work while I write my unit tests.

Thiosinamine answered 11/3, 2011 at 8:17 Comment(1)
Very-closely related, but not quite a duplicate I think due to some subtle differences: How can I convince Eclipse CDT that a macro is defined for source code editing and code completion?.Phytogenesis
G
21

You have to set the option "Build configuration for the indexer" to "Use active build configuration" in the projects C/C++ indexer preferences. Open the properties panel for the project, go to c/C++ General and Indexer and enable project specific settings and change the radiobutton on the bottom to "Use Active build configuration".

CDT indexer settings

Of course you can also set this in the global properties panel of Eclipse to change this setting for all projects.

Garderobe answered 4/6, 2012 at 20:34 Comment(4)
I will have to try this next time I use Eclipse (which currently looks like never to be honest). Looks like lots of others have had this same issue though. This could be the fix I was looking for!!Thiosinamine
Please leave a comment if this fixed it for you and also mention which version of Eclipse you used. Thanks!Thiosinamine
@Simon Lehmann I will buy you a beer if we meet someday! Big thanks. I am using CDT from couple of years but never spotted this option.Merriott
@Merriott Well, let's hope that someday we will be able to send beer over the internet :) Regarding this setting: I am actually still kind of puzzled why this isn't the default setting, as it makes much more sense.Garderobe
I
2

Go to your project's Properties, then go to C/C++ General -> Paths and Symbols -> Symbols. Add your defines there and it will work.

Impressionism answered 6/7, 2011 at 20:15 Comment(3)
They're there. Still don't work. Also it doesn't seem to change as I change the build configuration. I guess I'm assuming that it ought to, and maybe theres some setting somewhere that will let it update to the defines that are for the right build config.Thiosinamine
I also thought it wouldn't work, but I mistakenly added the symbol only for the "Assembler" part. Adding it for all languages (especially C) does it, and Eclipse doesn't gray out the parts anymore.Yacketyyak
I provide a very-detailed explanation of how to do this, including with screenshots, in my answer here: How can I convince Eclipse CDT that a macro is defined for source code editing and code completion?.Phytogenesis
D
1
  1. If you want to change the defines for your system based on build configuration, you need to do as dgrant said here: "Project properties and select C/C++ General -> Paths and Symbols then select the Symbols tab", but you need to make sure you have the correct configuration selected at the top of your window. Also be sure when you add the symbol to the list that you DON'T check the "add to all configurations" checkbox.
  2. If you want your correct set of symbols to be parsed by CDT in your editor, you'll also need to make sure you have the correct build configuration active as well. To do that, right-click on your project and select Build Configurations -> Set Active -> and select the build configuration you want to make active. CDT will now parse all the files as if this build configuration were active instead, using the global symbols you defined in the last step.

There is a problem I've seen with every version of Eclipse where it doesn't always decide to rebuild your index files immediately. To get it to do so, you can either start a build (the build doesn't actually have to complete), or you can right-click on your project and go to Index -> Rebuild. Both cause an immediate re-indexing to occur. You shouldn't need this, however. I can confirm that simply changing the build configuration as described in step 2 above will cause an #ifdef I have in my code which checks for a symbol defined only in one of my build configurations to immediately become greyed-out or un-greyed-out, as it should.

Dhoti answered 2/10, 2012 at 18:35 Comment(0)
P
0

Check provider: -> C/C++ General -> Preprocessor Include Paths, Macros etc. -> Providers -> CDT Managed Build Settings Entries. It should be enabled.

Check if defined symbol is in entries of this provider.

Indexer is optional.

Pimp answered 27/8, 2020 at 12:32 Comment(0)
T
-4

My satisfactory solution has been to move away from Eclipse to editors with a working libclang plugin: Sublime Text, Vim.

For an intelligent IDE for C/C++, one probably can't go wrong with XCode or MSVC.

Thiosinamine answered 8/7, 2013 at 13:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.