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
LPetr.1
Senior

Now I am getting more and more confused. It is very strange that such a simple task is so complex to achieve

LPetr.1
Senior

I have also tried the following:

#define DEBUG 0
 
#ifdef DEBUG
	  #define DEBUG_PRINT(X) printf X
#else
	  #define DEBUG_PRINT(X)
#endif

I have manually set the DEBUG variable to 0 and so the debug printf should not work if I understand this correctly. However, it is still printing

I have an RTOS task which is printing "test" periodically

void StartDefaultTask(void const * argument)
{
  /* USER CODE BEGIN 5 */
  /* Infinite loop */
  for(;;)
  {
	 DEBUG_PRINT(("test\n"));
	 osDelay(1000);
  }
  /* USER CODE END 5 */
}

st

It wont let me go to definition. Clicking go to declaration doesnt go anywhere.0693W00000Nqe4sQAB.png

It should be simple - DEBUG should just be defined for the 'Debug' configuration in the Project settings.

But that fact that it's not working shows that something's gone wrong ...

If those prints are still appearing, DEBUG must be getting defined somewhere.

Or you're not loading the code you think you are.

0693W00000Nqe4iQAB.pnghave you checked that?

I am definately loading the right code because each time I change printf message and I can see it update.

I cant find this property page

"I have manually set the DEBUG variable to 0 and so the debug printf should not work if I understand this correctly"

You misunderstand.

I explained this earlier:

#define DEBUG 0

that is a definition of DEBUG; you have defined it to be zero - so it is defined!

Therefore

#ifdef DEBUG

will be true - so all the prints will be enabled.

"'ifdef" means "if defined"

If you want to test the value that has been defined, you need:

#if DEBUG == 1
	  #define DEBUG_PRINT(X) printf X
#else
	  #define DEBUG_PRINT(X)
#endif

0693W00000Nqe6tQAB.png

LPetr.1
Senior

Ok I understand now. I did the following:

For release I define debug 0

0693W00000Nqe6yQAB.png 

For debug I define debug 1

0693W00000Nqe73QAB.png 

Then in my main.c:

#if(DEBUG==1)
	  #define DEBUG_PRINT(X) printf X
#else
	  #define DEBUG_PRINT(X)
#endif

Now the DEBUG_PRINT only works when I am in debug mode exactly how I want to!

Thank you very much for your help

Note that it is more conventional for DEBUG to be either defined or not, and to test it with #ifdef.

Anyhow, if the issue is now resolved, please mark the solution:

0693W000008y9fZQAQ.png 

Instead of ==1 one of these should be used:

#if DEBUG != 0
#if DEBUG

Anyway, #ifdef is, of course, the most appropriate here.