2024-11-04 05:45 PM
I have a bunch of debugMsg() calls throughout my code. Is there a way to automatically strip those out during a build?
2024-11-05 04:58 PM - edited 2024-11-06 12:34 AM
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));