cancel
Showing results for 
Search instead for 
Did you mean: 

Multiple definition error after STM32CubeIDE 1.9.0 update

Rodolfo_Rodrigues
Associate

I get a multiple definition error for every variable declared inside a .h file after updating from CubeIDe 1.8.0 to 1.9.0. The previous CubeMX version was 6.3.0 and now it is 6.5.0. Before updating there would be no errors while building the code. The .h file has the header guards generated by the IDE. Is there any way to fix this?

1 ACCEPTED SOLUTION

Accepted Solutions
CButz.1
Associate III

See https://gcc.gnu.org/gcc-10/porting_to.html

"A common mistake in C is omitting extern when declaring a global variable in a header file. If the header is included by several files it results in multiple definitions of the same variable. In previous GCC versions this error is ignored. GCC 10 defaults to -fno-common, which means a linker error will now be reported. To fix this, use extern in header files when declaring global variables, and ensure each global is defined in exactly one C file. If tentative definitions of particular variables need to be placed in a common block, __attribute__((__common__)) can be used to force that behavior even in code compiled without -fcommon. As a workaround, legacy C code where all tentative definitions should be placed into a common block can be compiled with -fcommon"

.

View solution in original post

20 REPLIES 20
CButz.1
Associate III

See https://gcc.gnu.org/gcc-10/porting_to.html

"A common mistake in C is omitting extern when declaring a global variable in a header file. If the header is included by several files it results in multiple definitions of the same variable. In previous GCC versions this error is ignored. GCC 10 defaults to -fno-common, which means a linker error will now be reported. To fix this, use extern in header files when declaring global variables, and ensure each global is defined in exactly one C file. If tentative definitions of particular variables need to be placed in a common block, __attribute__((__common__)) can be used to force that behavior even in code compiled without -fcommon. As a workaround, legacy C code where all tentative definitions should be placed into a common block can be compiled with -fcommon"

.

mattias norlander
ST Employee

@Rodolfo_Rodrigues​, do you find these issues in CubeMX generated code?

If, so please let us know Cube package, revision, files, lines, ...

Then I can file some tickets and dispatch to the right teams for a first analysis of the scope.

No, it only happened with the code i've written. After applying what @CButz.1​ said in his comment the errors were gone.

PVand.9
Associate II

I have this too with all my projects. I returned to v.1.8.0. for the moment

I have one .h-file that is included from several .C-files.

Therefor I put this on the beginning of each .h-file:

#ifndef PMT_H_

#define PMT_H_ 1

// declarations come here

#endif /* PMT_H_ */

So, if this .h-file is included several times, the declarations are only done once: the first time it is included.

This worked before version 1.9.0, and not any longer with 1.9.0

Wouldnt there be a solution? Why is the if-statement not working?

the toolchain changed to gcc 10 and therefore tentative definitions in header files are no longer ignored.

You should not define variables in headers, but rather use extern in header files and define it in one source file. This has always been a error, but was ignored in gcc 9. So to fix this either use -fcommon

to compile or the better solution is to declare the variables in the header as "extern" and define them in one source file. Heres the link to the GNU GCC 10 source:

https://gcc.gnu.org/gcc-10/porting_to.html

As this problematic isnt bound to cubeide but rather C and gcc 10 here is a external link that maybe helps:

https://stackoverflow.com/questions/1164167/variable-declaration-in-a-header-file

Thanks for the clarification!

Ghost
Associate

Apparently this is some new bug. had it just after updating the project to the new version.

Solution that worked for me: open project properties -> settings, under MCU GCC Compiler select Miscellaneous. Click add flag -fcommon

Thanks! It works!

AKara.10
Associate

Thanks a lot.