cancel
Showing results for 
Search instead for 
Did you mean: 

Strip debug msg function calls during build

pulsar
Associate II

I have a bunch of debugMsg() calls throughout my code.  Is there a way to automatically strip those out during a build?

1 REPLY 1
Andrew Neil
Evangelist III

The usual way is to have something like:

 

#if defined DEBUG
#define debugMsg(x) output_stuff(x)
#else
#define debugMsg(x) 
#endif

 

thus debugMsg() effectively "disappears" in non-DEBUG builds.

 

This is general C practice - not specific to STM32 or CubeIDE.

 

Addendum:

Variadic macros became a standard part of the C language with C99 (and GCC had proprietary support before that).

This lets you do, for example:

 

#if defined DEBUG
#define debugMsg(...) printf( __VA_ARGS__ )
#else
#define debugMsg(...) 
#endif

 

 https://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html 

 

Before that, there were tricks like this:

 

#ifdef DEBUG
# define DEBUG_PRINT(x) printf x
#else
# define DEBUG_PRINT(x)
#endif

 

To use that DEBUG_PRINT macro, you had to add extra parentheses; eg,

 

DEBUG_PRINT(("var1: %d; var2: %d; str: %s\n", var1, var2, str));

 

https://stackoverflow.com/a/1941337