cancel
Showing results for 
Search instead for 
Did you mean: 

how write c++ and include c++ lib in stm32CubeIDE?

kshin.111
Associate III

hi

in stm32CubeIDE c++ code and standard lib (#include <iostream> or #include <iostraem.h>) not work.

i test:

'convert C to C++'

make c++ project

use only c++ code in func main() ==> 'bool a = true;'

all test not woke!!!

ya khoda

3 REPLIES 3
KnarfB
Principal III

Create a C++ project from scratch. By default, C++ code is valid in .cpp files only. So you need to add your C++ code in a new file, say main_cpp.cpp. To keep the generated code intact (if you like HAL...), add a new header file, say main_cpp.h

#ifndef INC_MAIN_CPP_H_
#define INC_MAIN_CPP_H_
 
#ifdef __cplusplus
extern "C" {
#endif
 
void main_cpp();
 
#ifdef __cplusplus
}
#endif
 
#endif /* INC_MAIN_CPP_H_

and include it in main.c. In main(), right before the while loop in a user code section, call main_cpp();

Implement void main_cpp() in main_cpp.cpp.

> #include <iostream>

C++ streams? Where do you expect the output going to? For POSIX IO, adapt syscalls.c

> #include <iostream>

C++ streams? Where do you expect the output going to? For POSIX IO, adapt syscalls.c

only for add one lib of c++ use of <iostream> 😅

thank you

Pavel A.
Evangelist III

If you want to use bool in C files such as main.c, just #include <stdbool.h>

-- pa