I need to be able to build different versions of my code, one for debugging and one for “release”, for example.

The standard mechanism for doing this is to define, or leave undefined, various symbols that cause the compiler to include, or exclude chunks of source code.  The symbols are not in the code, but are submitted to the compiler when it is called.   So called the compiler with the flag -Drelease inserts a symbol called “release” in the compiler’s symbol table before it gets started.  Then during compilation the compiler can conditionally switch in, or out, chuncks of code based on whether this is “release” code or not.

Eclipse works this way too – only it doesn’t.

Here is a very simple program, to illustrate my vexing issue.   My conditional compilation variable is called “FOO”

The program is silly, and is designed to fail during compilation if x=0.  (If x=0 the compiler will detect that the program is null, and will signal a compiler error.)
The important part is at the top.   It says that if a symbol called FOO has been previously defined then the compiler should compile the statement “int x = 1”, and if FOO is not defined it should instead compile the statement “int x = 0”.
Elsewhere in the property definitions for this program I have defined FOO.   
Eclipse kindly grays out the code that it is going to ignore.  In this example something has gone wrong because Eclipse implies that x=1, but the compilation fails (little yellow exclamation mark) because clearly x=0.
Whatever I do, I get the compiler error.   What a time-waster Eclipse is!

Leave a Reply