Become a leader in the IoT community!
Join our community of embedded and IoT practitioners to contribute experience, learn new skills and collaborate with other developers with complementary skillsets.
Join our community of embedded and IoT practitioners to contribute experience, learn new skills and collaborate with other developers with complementary skillsets.
I am currently utilizing an STM32G431KB board that differs from other boards in the STM32 Nucleo series as it has a wired SWO. While setting up SWV printf on a Nucleo variant of the same product, I encountered an inquiry and implemented suggestions given by its first answer which allowed me to enable functioning SWV under C programming language.
However, upon switching over to C++, no output is visible during usage of this feature.
For the C project, I created a new project, switched Debug to “Trace Asynchronous SW,” and added the following code:
/* USER CODE BEGIN Includes */
#include "stdio.h"
/* USER CODE END Includes */
/* USER CODE BEGIN 0 */
int _write(int file, char *ptr, int len)
{
int DataIdx;
for (DataIdx = 0; DataIdx < len; DataIdx++)
{
ITM_SendChar(*ptr++);
}
return len;
}
/* USER CODE END 0 */
In the main loop, I added:
/* USER CODE BEGIN 2 */
int i = 0;
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
printf("%d Hello World!n", ++i);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
Upon activating SWV in the Debug Configuration, adjusting the core clock to 170 MHz, deactivating timestep in SWV settings and empowering port 0; executing C yields optimal results with expected output.
On renaming `main.c` to `main.cpp`, the project operates in C++, but there is no result. What could be the reason for this problem?
hi, @ifreakio the issue you’re facing is likely due to name mangling in C++. You can fix this by declaring the `_write` function with `extern “C”`, which makes sure the C standard library recognizes it correctly. This should get your SWV printf output working as expected
Thanks @darkai042 , I am gonna try using `extern “C”` to resolve the name mangling issue. Just to clarify, should I apply `extern “C”` to the `_write` function only, or are there other parts of the code that also need this adjustment in C++?
CONTRIBUTE TO THIS THREAD