Skip to main content
Associate II
November 5, 2024
Question

Strip debug msg function calls during build

  • November 5, 2024
  • 1 reply
  • 798 views

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

1 reply

Andrew Neil
Super User
November 6, 2024

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 

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.