header with variable referenced more than twice
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-12-03 9:00 AM
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.
Solved! Go to Solution.
- Labels:
-
STM32CubeIDE
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-12-03 10:56 AM
It's because the line is a declaration and a tentative definition.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-12-03 9:11 AM
Please show the code you're using, as the answer depends on the specifics.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-12-03 9:37 AM
#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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-12-03 10:53 AM
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.
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-12-03 10:56 AM
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. ​
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-12-03 10:56 AM
It's because the line is a declaration and a tentative definition.
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.
