Conditional Compilation And You

Conditional Compilation is one of those parts of programming that sit in the dusty corners of the knowledge banks of programmers world-over, yet is one of the most ingenious additions to any language. Usually something that was reserved for C/C++ programmers, with the power of freeBASIC's new preprocessor, you can now use conditional compilation to help your program.

The preprocessor allows you flexibility in changing the way code is generated through the use of conditional compilation. Take this scenario: you are debugging the code in your program, and you want to add some extra code to output a few variables, but remove them in the final version. The code would be something like this:

#define DEBUG

#ifdef DEBUG
Print "Debug Value"
#endif 'DEBUG

Note you do not need the comment after the #endif, but is it good practice.

Basically, the above code checks to see whether DEBUG has been defined, and if it has, then the code between the #ifdef...#endif will be executed. While this may seem silly, the uses this has are amazing. If you simply remove one line at the top of your program (#define DEBUG), then all the 'debug code' that you've added won't be sent to the compiler -- the preprocessor removes it, reducing the bloat of the final executable.

'Turn on debugging
#define DEBUG

'Turn off debugging
#undef DEBUG

The #undef directive is a way of 'undefining' something, in this case DEBUG. While it is strictly not needed (just commenting out the line '#define DEBUG' is enough), it makes the code much clearer, and has other uses:

#ifndef DEBUG
Print "Production Version"
#endif 'DEBUG

While not the most useful example, this demonstrates the use of another directive: #ifndef. This directive will cause the code to be compiled if the symbol is not defined.

Much like a normal programming language, the sense of the conditional can be reversed using a variant of else, #else:

#ifdef DEBUG
Print "Test Version"
Print "Production Version"
#endif 'DEBUG

Of course, there are many applications to this. Who says you need to do this on debug code only? You could actually check the effect of a new piece of code, or some test routines by simply defining a name like TESTCODE and using the preprocessor directives to encompass your code for conditional compilation:

#define TESTCODE


The scope of this tutorial is a limited one, but this method is used by professionals. It makes life easy when programming. I have used this method in my own code. To see this code in action, view the source

Avinash 'aetherFox' Vora
avinashvora [at] gmail [dot] com.