Steven Lu
Steven Lu

Reputation: 43457

Eclipse CDT Does not track macro defines correctly

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.

Upvotes: 13

Views: 13198

Answers (5)

mtalexan
mtalexan

Reputation: 763

  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.

Upvotes: 1

dgrant
dgrant

Reputation: 1427

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

Upvotes: 2

LCDM
LCDM

Reputation: 11

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.

Upvotes: 0

Steven Lu
Steven Lu

Reputation: 43457

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.

Upvotes: -4

Simon Lehmann
Simon Lehmann

Reputation: 10967

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.

Upvotes: 21

Related Questions