Search code examples
c++conditional-compilation

VC++ programatically exclude specific lines of code


I didn't found any reference to what I'm looking for so probably best is to ask here. Lets say I want to have 2 version of my program.

Now I do something like:

#define MY_VER 0 //0 or 1 depending on which version I want to compile

Function1();
if(MY_VER)
Function2();  //It will run only if MY_VER is set

Now I have lots of code as such, but even when I compile it with MY_VER=0 some of the strings in "my" functions are still in exe. How to programaticaly exclude some of the lines, so they are completly unexistant when I want it to be like that.


Solution

  • One obvious possibility would be to use the preprocessor:

    #if MY_VER
    Function2();
    #endif
    

    Less obvious (but usually equally effective) is to just turn on the optimizer. What you have there is dead code, which most compilers can identify and remove quite effectively.