Skip to main content
LPetr.1
Senior
May 25, 2022
Solved

STM32CubdeIDE printing messages to UART only when in DEBUG mode

  • May 25, 2022
  • 7 replies
  • 5441 views

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?

This topic has been closed for replies.
Best answer by Andrew Neil

"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

7 replies

Andrew Neil
Super User
May 25, 2022

First, Are you sure that you actually did a full rebuild, and that you actually loaded the correct binary?

Once that's confirmed, if the problem persists, then you need to investigate where the DEBUG symbol is getting defined in the two buillds.

Note that it's common to have something like:

#ifdef DEBUG
	 #define DEBBUG_PRINT(X) printf X 
#else
	 #define DEBBUG_PRINT(X)
#endif

which you then use as:

DEBUG_PRINT(("stuff"));

which clutters the source less.

See, for example, https://stackoverflow.com/questions/1644868/define-macro-for-debug-printing-in-c

But that relies upon your DEBUG symbol working first...

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.
LPetr.1
LPetr.1Author
Senior
May 25, 2022

Unfortunately I still cannot get this to work. This is probabaly because my Debug symbol is not working properly as you have suggested..

Is there any way to test this? Do I need to change something in my project settings to ensure the DEBUG symbol to work ( it should be set to 0 when I select release mode and should be 1 when I select debug mode).

I have implemented the code that you have suggested, then I have built my project using release mode:

0693W00000NqdhFQAR.png 

And then I just clicked "run"

0693W00000NqdhKQAR.png

Andrew Neil
Super User
May 25, 2022

"Is there any way to test this?"

STM32CubeIDE should shade code that's disabled by a #if

So try something like

#ifdef DEBUG
	 printf("debug \n");
#else
	 printf("non-debug \n");
#endif

"it should be set to 0 when I select release mode and should be 1 when I select debug mode"

No: You're testing whether it is defined, or is not defined.

If you're defining it as zero, then it is defined!

In that case, you'd need

#if DEBUG == 1
	 printf("debug \n");
#else
	 printf("non-debug \n");
#endif

Or, perhaps

#if DEBUG == 1
	 printf("debug \n");
#elif DEBUG == 0
	 printf("non-debug \n");
#else 
#error "DEBUG must only be set to 0 or 1"
#endif

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.
LPetr.1
LPetr.1Author
Senior
May 25, 2022

I am not defining DEBUG myself. I want the program to automatically recognise whether it is debug or release mode and set this DEBUG flag for me accordingly

Andrew Neil
Super User
May 25, 2022

If your prints are still printing, then it must be getting defined somewhere!

Do you get the shading correctly?

What happens if you do a 'Go to definition' on DEBUG ?

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.
LPetr.1
LPetr.1Author
Senior
May 25, 2022

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

LPetr.1
LPetr.1Author
Senior
May 25, 2022

Do I need to add DEBUG symbol here?:

0693W00000NqdrZQAR.png

Andrew Neil
Super User
May 25, 2022

That's showing the 'Release' configuration - so you want DEBUG to not be defined.

Which seems correct.

Note that there are separate settings for C, C++, and Assembler.

It's common to define NDEBUG as a complement to DEBUG - so you might add that here.

NDEBUG and DEBUG should be mutually exclusive - you could check with

#if defined( DEBUG ) && defined( NDEBUG )
#error "Both DEBUG & NDEBUG are defined"
#endif

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.
LPetr.1
LPetr.1Author
Senior
May 25, 2022

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

Andrew Neil
Super User
May 25, 2022

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?

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.
LPetr.1
LPetr.1Author
Senior
May 25, 2022

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

LPetr.1
LPetr.1Author
Senior
May 25, 2022

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

Andrew Neil
Andrew NeilBest answer
Super User
May 25, 2022

"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

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.
LPetr.1
LPetr.1Author
Senior
May 25, 2022

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

Andrew Neil
Super User
May 25, 2022

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 

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.
Piranha
Principal III
May 25, 2022

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

#if DEBUG != 0
#if DEBUG

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