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?

1 ACCEPTED SOLUTION

Accepted Solutions

"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

View solution in original post

21 REPLIES 21
Andrew Neil
Evangelist III

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...

LPetr.1
Senior

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

"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

"I have built my project using release mode"

I'm not sure if just changing Debug/Release will force a full rebuild?

To be sure, do a Clean first, and then rebuild.

Check the timestamps on the output files...

LPetr.1
Senior

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

LPetr.1
Senior

Do I need to add DEBUG symbol here?:

0693W00000NqdrZQAR.png

I have deleted all debug/release folders and did a fresh build so that should be fine..

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

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 ?