2022-05-24 09:54 PM
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?
Solved! Go to Solution.
2022-05-25 04:16 AM
Now I am getting more and more confused. It is very strange that such a simple task is so complex to achieve
2022-05-25 04:23 AM
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
2022-05-25 04:26 AM
It wont let me go to definition. Clicking go to declaration doesnt go anywhere.
2022-05-25 04:26 AM
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.
have you checked that?
2022-05-25 04:29 AM
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
2022-05-25 04:32 AM
"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
2022-05-25 04:38 AM
2022-05-25 04:40 AM
Ok I understand now. I did the following:
For release I define debug 0
For debug I define debug 1
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
2022-05-25 04:47 AM
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:
2022-05-25 06:30 AM
Instead of ==1 one of these should be used:
#if DEBUG != 0
#if DEBUG
Anyway, #ifdef is, of course, the most appropriate here.