1

Using Eclipse IDE. Problem is that #defines aren't passing from the headers to the c and h files. Not sure if it's a project settings thing or what, but Visual Studio IDE is not giving me any trouble on the same project. Some of the projects in Eclipse don't have this problem though. Any ideas on what may be wrong? See example of the problem below.

Even though EXAMPLE is defined in header1.h, it is not recognized as defined in main.c. There is no issue with the compiler finding the headers either. Thanks for the help all.

Header1.h

#define EXAMPLE 1

main.c

#include "Header1.h"

#if defined(EXAMPLE)

/* code here */

#endif
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Nick
  • 11
  • 4
  • This seems like a pretty basic feature that could go wrong; can you pare down your problem into something that someone else could easily test? – sarnold Nov 09 '11 at 04:01

3 Answers3

0

Behavior similar to that in the question can be seen when using #defines that are strings, not integers.

The following code seems like it should run Code(); only when MODE is equal to the string different:

mode.h

#define MODE something

different.cpp

#include "mode.h"

#if MODE == different
  Code();
#endif

Eclipse shows Code(); as being active when it appears it should be inactive. The reason for this is that the preprocessor does not support string comparisons, only integer ones1.

When mousing over MODE in different.cpp, MODE is shown as having the value something. Although this is technically correct, it can be misleading since both MODE and something evaluate to the same thing (a defined but empty value). Given that they both have the same value (nothing) they evaluate as being equal and Code(); is run.

1 This is gone into in more detail in this question.


Solutions

Two possible ways of correctly handling this come to mind:

  • Assign numeric values to each option
  • Use unique #defines for each option

Use numeric values

The code could be written as follows:

mode.h

#define MODE_something 0
#define MODE_different 1

#define MODE MODE_something

different.cpp

#include "mode.h"

#if MODE == MODE_different
  Code();
#endif

In this case the code works as expected since MODE and MODE_different evaluate to two distinct values (0 and 1, respectively).

Use unique #defines

Another approach is to use uniquely-named macros for each option. For example:

mode.h

// Select the active mode:

#define MODE_something
//#define MODE_different

different.cpp

#include "mode.h"

#ifdef MODE_different

Code();

#endif
Alex Hajnal
  • 213
  • 2
  • 8
0

You mention there is no issue with the compiler and visual studio, so assuming you are referring to the code being grayed out in the Eclipse IDE? If so, check the Indexer settings

Preferences --> C/C++ --> Indexer

Check Enable Indexer Check Index Source files not included in the build Check Automatically update the index Check Use Active build Configuration

etc,...

hope this was relevant to your issue.

Tom
  • 539
  • 4
  • 7
0

Does this work?

#ifdef EXAMPLE

instead of

#if defined(EXAMPLE)