29

I'm using eclipse to work on some c code and it is not indexing code inside conditional compilation blocks like this:

#ifdef USE_FEATURE_A
int feature_a(...) {
   some = code(here);
}
#endif

How can I get eclipse to index the feature_a function?

Arthur Ulfeldt
  • 90,827
  • 27
  • 201
  • 284

6 Answers6

35

You could tell eclipse that USE_FEATURE_A is defined. Open your project properties and go to the "C/C++ General->Paths and Symbols" page, under the "Symbols" tab click the "Add" button and put USE_FEATURE_A in the name feild and click OK.

Note: this will cause it not to index any #else sides to your preprocessor stuff... so unless they are all like the one in question you can't AFAIK, but if they are they you're good. (Eclipse contains a C preprocessor that it uses to analyize your code all the stuff above does is essentially the same as adding -DUSE_FEATURE_A to your command line so Eclipse's preprocessor will behave differently from the one in your compiler)

Spudd86
  • 2,986
  • 22
  • 20
  • 3
    Thanks so very much for this, you literally just gave me back hours of my life! – Arthur Ulfeldt Aug 05 '10 at 19:11
  • 3
    Yes, that is a solution but far from "user friendly". This is a major problem for me. It´s not practical to go around all of the code (especially for code from others) and search for all the symbols and then add them to the project properties! I don't understand why Eclipse can't have a checkbox to allow us to disable the preprocessor for indexing/reference purposes... Compared to Visual Slick (that me and my colleagues used before) this is a major disadvantage of Eclipse. (C/C++ version). – Rex Dec 07 '12 at 11:45
  • 1
    the only problem is that there may be several defines for this. i mean several... – Vetras Feb 28 '13 at 11:30
  • also on eclipse website http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.cdt.doc.user%2Ftasks%2Fcdt_t_proj_paths.htm – Oleg Vazhnev Sep 24 '14 at 09:07
  • 1
    Note: If you use multiple build configurations and Eclipse doesn't behave as you expect, go to C/C++ General->Indexer and make sure that "Build configuration for the indexer" is set to 'Use active build configuration" if desired, or you can force it to a specific fixed configuration option. – Digicrat Apr 05 '17 at 20:56
4

This is an easier and in my opinion more elegant solution to the one selected as the solution:

If someone has the same problem (as I had), this can (now?) easily be solved by going to Window->Preference->C/C++/Indexer and enable "Index all header variants". Then click Project->C/C++ Indexer->rebuild and clean and build your project. This should resolve all error originating from preprocessor commands.

antibus
  • 983
  • 1
  • 11
  • 16
  • 1
    This doesn't help with conditional compiling. Code blocks in #else are still greyed out. – Manas Jan 07 '19 at 03:30
2

For what it's worth, getting eclipse to parse conditionally compiled code is much harder to do than would appear at first glance. I found a paper on by IBM from 2007 where they said they will prioritize for the "next release".

Handling Conditional Compilation in CDT's Core

craigster0
  • 483
  • 3
  • 9
  • thanks for that information. it was what i was looking for. basically, appart from the Spudd86 response, there is no way to get eclipse to proccess this code. – Vetras Feb 28 '13 at 11:31
1

I had this same problem, but the code conditionally eliminated by preprocessing was perfectly valid c code and I wanted it formatted... This was my solution:

1) Global find/replace of #if to #JUNKif

2) Ctrl-Shift-F to reformat the source

3) Another global find/replace of #JUNKif to #if

0

One way to index code under flag in Eclipse(Kepler) c/c++Editor.

You can enable the compilation flags in Eclipse editor so that code under them can be indexed.

Properties > Preprocessor Include Paths > CDT User settings Entries

Click on ADD and add the Preprocessor Macro and you can specify its value.

Pieter
  • 895
  • 11
  • 22
akshaypmurgod
  • 77
  • 1
  • 3
0

Best way I guess is to use the Indexer option : Project Properties>C/C++ General>Indexer. You can choose Enable project specific settings I prefer choosing "Use active build configuration" so that all files which are actually built in the project are indexed. Anyhow you can also choose to index all files in the project even if they are not included in the build ...