cancel
Showing results for 
Search instead for 
Did you mean: 

[SOLVED] Weak variables of global file scope linker error

J.Christ
Associate II

So I'm trying to gain greater understanding on the STM32 tools on a project that I've inherited. I'm noticing that there are weak variables that are of global scope to a given file that are defined in multiple files such as follows:

main.c:

int g;

other.c

int g;

I would expect that when the project is linked that one of the variable is chosen and no linker errors are give, however, a linker error appears as if the variables are strong. Something along the line of the following:

.../main.o:/project/other.c:2: multiple definition of `g'; .../main.o:/project//main.c:2: first defined here

I have seen that there are compiler switches in gcc "-fno-common" that can affect if this error is thrown with weak variables but I can't seem to find this turned on in my project.

The real issue at play here is that when a colleague builds the project in Windows from the same git repository they do not encounter this issue, but I see it when I compile in Linux. Further, this is a massive legacy project that generates over 400 of these link errors. Though I am able to resolve the issues by making a single weak variable and making all other instants extern, I first would like to understand the code before potentially breaking it.

If there is anyone that can help resolve this issue I would appreciate it.

Jacob

4 REPLIES 4
KnarfB
Principal III

What tools+versions are you exactly using?

The C term for what you describe is "multiple tentative definitions of a variable".

You may need to specify -fcommon for gcc. See https://gcc.gnu.org/gcc-10/porting_to.html

and https://stackoverflow.com/questions/67270121/why-can-i-define-a-variable-twice-in-c

hth

KnarfB

J.Christ
Associate II

I have the same results with "GNT Tools for STM32 (10.3-2021.10)" that is included with CubeMX IDE and "arm-none-eabi-9-2020-q2".

Jacob

KnarfB
Principal III

And -fcommon specified for compilation?

J.Christ
Associate II

Adding -fcommon to the GCC Compiler Misc flags did the trick (thanks by the way). Next task is to figure out why my colleague's project builds without this flag from the same git repo. I suspect an older tool chain.

Jacob