2022-12-07 11:51 AM
Strange problem again.
I created a new empty STM32 project and selected C++. Now I made two functions
void test(uint8_t a)
{
}
void test(uint16_t a)
{
}
But I get an error of redefining the function test. The file (like all source files) has the ending .c and if I change it to .cpp then it works. But if I change any of my other source files to .cpp the functions in those files are not found any more. If I leave the ending .c then I cannot overload functions. What is going on here???
Solved! Go to Solution.
2022-12-08 01:48 AM
The STM HAL is written in plain C. And the main.c file will be in C too.
I don't know what the C++ switch on creation works :smirking_face: Maybe it comes from standard Eclipse window "create a new project". I was confused about it too.
You can write you own code in full C++ and call to it (in plain C style with the extern C linkage).
Do you have played with some STM tutorials/examples? They have a nice way to do it.
The picture shows my way (based on the STM idea). My "application", "Middleware" and "Driver" are written in C++.
The canInterface-"Driver" based on the plain C HAL.
2022-12-07 12:07 PM
This is very basic.
STM32CubeIDE is based on gcc compiler.
*.c files will be compiled as plain C and *.cpp will be compiled with C++.
Overloading is a C++ thing and not allowed in plain C.
If you use C++ you must pay attention on the namespace of a function. Maybe you must deal with it over the header file where you publish you functions. But keep in mind you can't use the overloading inside a *.c file like the main.c.
2022-12-07 04:05 PM
If you want to use a C++ function (in a *.cpp file) within C, you need to define them with C linkage using extern "C".
2022-12-07 10:59 PM
Thanks for the replies. I know only c++ supports overloading. The point is that I created this project as a C++ project. So I expected all files to be .cpp in the first place. What difference does it then make if I create the project as C or C++ project? I would like everything to be C++.
2022-12-08 01:48 AM
The STM HAL is written in plain C. And the main.c file will be in C too.
I don't know what the C++ switch on creation works :smirking_face: Maybe it comes from standard Eclipse window "create a new project". I was confused about it too.
You can write you own code in full C++ and call to it (in plain C style with the extern C linkage).
Do you have played with some STM tutorials/examples? They have a nice way to do it.
The picture shows my way (based on the STM idea). My "application", "Middleware" and "Driver" are written in C++.
The canInterface-"Driver" based on the plain C HAL.
2022-12-08 04:04 AM
Thank you for the detailed answer. I still get the "redefined" error for the functions in some files. Strangeley in some it does work (?). I have not idea what is going on. I never had this strange behaviour any where else. I guess I will just stick to c for now.
2022-12-08 05:14 AM
You must not use overloading (or other C++ specific things inside the headers that you include to a plain C file.
If you want to use only one header file there must be a wrapper function and excludion of the C++ stuff
For example this header should work:
#ifndef __inclusion_protection__
#define inclusion_protection
#ifdef __cplusplus
extern "C" {
#endif
/* one function is ok.
Maybe this one it handles the bigest value */
void test(uint16_t a);
#ifdef __cplusplus
}
#endif
/* all coming up will be ignored in plain C */
#ifdef __cplusplus
/* this is a C++ function */
void test(uint8_t a);
#ifdef __cplusplus
#endif /* inclusion_protection */
2022-12-21 11:37 PM
Thank you for the explanation.