cancel
Showing results for 
Search instead for 
Did you mean: 

I cannot overload a simple function in STM32CubeIDE with empty C++ project

Nickelgrass
Senior

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???

1 ACCEPTED SOLUTION

Accepted Solutions

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 �� 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.0693W00000WKXMDQA5.png

View solution in original post

7 REPLIES 7
TManiac
Associate III

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.

TDK
Guru

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".

If you feel a post has answered your question, please click "Accept as Solution".
Nickelgrass
Senior

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++.

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 �� 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.0693W00000WKXMDQA5.png

Nickelgrass
Senior

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.

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 */

Nickelgrass
Senior

Thank you for the explanation.