How to disable parts of code when compiling

You have two options: preprocessor and source choice.

Preprocessor is #ifdef, usually by defining a macro in different variants depending on platform, like this:

#if defined(EMBEDDED)
#  define LOG(msg)
#else
#  define LOG(msg) log(msg)
#endif

and then using the macro to log things:

LOG("I'm here");

The macro can of course be more complex.


Source choice means, basically, that you replace your logging library with a substitute that has the same interface, but does nothing.

Source choice is easier to manage and a bit cleaner to use, but not as flexible or thorough. For really minimizing your executable size, you probably want to go the preprocessor way.


Source choice would still make the calls to the function so for an embedded system may not be the most optimized. You might also be able to change the path to enable source choice instead of copying libraries in/out.

Tags:

C++