Skip to main content
JCuna.1
Senior
December 3, 2021
Solved

header with variable referenced more than twice

  • December 3, 2021
  • 5 replies
  • 2975 views

When I create libraries, sometime I forget create the variable in .c file and then write the same in header file with extern. I just create the variable in header file, and access to his from .c file and even from other libraries. IAR compiler warn this, however stm32cube ide with gcc compiler does not. I don't know if there is option to make the compiler more strict with mistakes like this. I already enable warn for implicit conversion flag which is helpful.

This topic has been closed for replies.
Best answer by TDK

It's because the line is a declaration and a tentative definition.

https://stackoverflow.com/questions/15734699/in-c-why-is-multiple-declarations-working-fine-for-a-global-variable-but-not-for

In C, "int x;" in the file scope (outside any function) is a declaration and a tentative definition. Multiple tentative definitions are allowed; only one definition is allowed.

If you instead initialized it with a value, it is also a definition and the linker will error out due to multiple definitions.

You can have GCC error out instead if you compile with the "-fcommon" flag.

5 replies

TDK
December 3, 2021

Please show the code you're using, as the answer depends on the specifics.

"If you feel a post has answered your question, please click ""Accept as Solution""."
JCuna.1
JCuna.1Author
Senior
December 3, 2021
#include "my_lib.h"
 
void my_lib_something(void){
 my_lib_var++;
}
#include "main.h"
 
uint8_t my_lib_var;
 
void my_lib_something(void);

these are my_lib.c and my_lib.h.

then I call my_lib.h in two different libraries:

first call in "another_library"

another_lib.h

#include "main.h"
#include "my_lib.h"
 
uint8_t antoher_lib_var;
 
void anther_lib_something(void);

another_lib.c

#include "another_lib.h"
 
void another_lib_something(void){
 another_lib_var = my_lib_var + 1;
}

second call in "another_library_2"

another_lib.c

#include "main.h"
#include "my_lib.h"
 
uint8_t antoher_lib_var_2;
 
void anther_lib_something_2(void);

another_lib.h

#include "another_lib_2.h"
 
void another_lib_something_2(void){
 another_lib_var_2 = my_lib_var + 1;
}

these are creating twice the variable another_lib_var because it is referenced the same library two times in anothers libraries as IAR explain.

Tesla DeLorean
Guru
December 3, 2021

Learn how to use extern when defining variables in include files

If all your definitions use extern, the linker should allocate the variable once.

Or put the variable assignments in a single file..

Use statics if you need them in different/multiple namespaces.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
JCuna.1
JCuna.1Author
Senior
December 3, 2021

As I say, I know this is not correct. However GCC does not rise any warn. IAR warn me that there is a duplicate variable reference. I want that warning in GCC. ​

TDK
TDKBest answer
December 3, 2021

It's because the line is a declaration and a tentative definition.

https://stackoverflow.com/questions/15734699/in-c-why-is-multiple-declarations-working-fine-for-a-global-variable-but-not-for

In C, "int x;" in the file scope (outside any function) is a declaration and a tentative definition. Multiple tentative definitions are allowed; only one definition is allowed.

If you instead initialized it with a value, it is also a definition and the linker will error out due to multiple definitions.

You can have GCC error out instead if you compile with the "-fcommon" flag.

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