cancel
Showing results for 
Search instead for 
Did you mean: 

STM32CubdeIDE printing messages to UART only when in DEBUG mode

LPetr.1
Senior

Hello. I would like to print certain messages to UART only in DEBUG mode. These prints will not be necessary in release mode.

I have tried the following:

#ifdef DEBUG
	  printf("hello from debug \n");
#endif

And then I have built the project for both release and debug but the message is printed in both cases. How can I only get message to print in debug mode and not in release mode?

21 REPLIES 21

@Piranha​ "#ifdef is, of course, the most appropriate here"

Not in this case, where DEBUG is always being defined - as either 0 or 1.

In which case I would still prefer something of the form:

#if DEBUG == 0
// whatever
#elif DEBUG == 1
// whatever else
#else
#error "DEBUG must be either 0 or 1"
#endif

so that there's no ambiguity or unexpected surprises if a value other than 0 or 1 gets used.

I did already note that it's more conventional to only define DEBUG when needed, and test with #ifdef - but OP seems to prefer having it always defined, and checking the value.

Yes - for "Debug" configuration, not "Release" as seen on your screen.