cancel
Showing results for 
Search instead for 
Did you mean: 

STM32CubeIDE & C++ - Redirected printf no longer works - How to use cout?

NW27
Associate III

Hi,

I have a "C" project where the printf detailed below works for outputting to a UART.

When I change the STM32IDE compiler to a C++ project, the printf no longer works.

/****************************************************
 * 			UART Redirect							
 * Re-directs printf() to the UART8 for display 	
 * on the console									
 ****************************************************/
int __io_putchar(int ch)
{
  HAL_UART_Transmit(&huart8, (uint8_t *)(&ch), 1, 0xFFFF);
  setbuf(stdout, NULL);
  return 1;
}
 
int __io_getchar(void)
{
  uint8_t ch;
  HAL_UART_Receive(&huart8, (uint8_t *)(&ch), 1, 0xFFFF);
  return (int)ch;
}

My understanding is that COUT should be used for C++ projects.

How do I get the cout (& cin) operational (Redirect) ??

// main.cpp
 
using namespace std;
 
#include <iostream>
 
int main()
{
// Usage
cout << "Some String";
}

Thanks.

11 REPLIES 11
FLuba.1
Associate III

"Make sure you put those functions within "extern C" or within a C source file so they have the right linkage."

Verry important notice. You are right sir.

Your codes helped me. Thanks a lot.