Skip to main content
Rodolfo_Rodrigues
Associate
Solved

Multiple definition error after STM32CubeIDE 1.9.0 update

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?

This topic has been closed for replies.
CButz.1
CButz.1Best answer
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"

.

21 replies

CButz.1
CButz.1Best answer
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 Technical Moderator

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

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question.
Rodolfo_Rodrigues
Associate

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

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?

CButz.1
Associate III

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

PVand.9
Associate

Thanks for the clarification!

Ghost
Visitor II

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

AKOLG.1
Visitor II

Thanks! It works!

AKara.10
Visitor II

Thanks a lot.

LFerr.7
Associate III

you are the best

DFran.10
Visitor II

Solved!

The problem was solved with upgrading to the last version of the HAL firmware package.

ETezc.1
Associate

Thankns! It realy works.

parsek_ka
Visitor II

parsek_ka_0-1732706679825.png