Reputation: 4869
I have a header file which has an extern variable declared.
So it will look something like this in this a.h file
extern uint16_t externVariable;
So I have 2 .c files b and c in which i want to access the extern variable in the a.h file. in both b and c .c files i have included a.h file already
When I did not declare the variable in b but declared in c (without the word extern) which is something like
uint16_t externVariable;
It works fine. But when its in both b and c i have some compilation error. Is there any way to resolve this?
Reason for doing this because I have code in a .c file which I want to divide the code into 2 different .c files for neatness and clarity.
Upvotes: 2
Views: 2445
Reputation: 5028
"extern" tells the C compiler this function or variable was declared elsewhere, so you can use them even without declare them in the header files and include .h in .c files. Beware, "extern" means this variable was NOT the initial declare, the initial declare MUST NOT have the extern keyword.
In your case, you declared an extern variable in one file without add the "static" keyword, so the variable's scope will be the whole project. Then you declared "uint16_t externVariable;" at 2 places, the complier thought they were all initial declares have the same name, so a conflict begun.
For example, you can do something like this:
Example 1:
a.c: uint16_t externVariable;
b.c: extern uint16_t externVariable;
Don't worry about your header file, you can use externVariable in both a.c and b.c.
Example 2:
a.c: #include "a.h"
b.c: #include "a.h"
a.h: uint16_t externVariable;
This works.
Example 3:
a.c: extern uint16_t externVariable;
b.c: uint16_t externVariable;
c.c: uint16_t externVariable;
This won't work since you declared externVariable at multiple places.
Example 4:
a.c: static uint16_t externVariable;
b.c: static uint16_t externVariable;
This WILL work since "static" keyword limited their scopes so they won't conflict.
Example 5:
a.c: static uint16_t externVariable;
b.c: static uint16_t externVariable;
c.c: extern uint16_t externVariable;
This still won't work since "static" keyword limited their scopes, and the complier can't find where you declared externVariable in c.c.
You can also view TCPL, it gave a bigger picture about these keywords, hope these examples can help :)
Upvotes: 1
Reputation: 25740
The idea of extern is to let the compiler know that this variable is allocated in a difference source file, so you do not need to declare it again in another source file. Just leave it in one file (I would say a.cpp since it is in a.h) and include the a.h and it will work from all of the .cpp files.
Upvotes: 0
Reputation: 64730
The variable should be declared in only one source-code file. (file b.c
in your example).
By declaring it extern
in a header file, or even at the top of multiple .c
files, you're essentially stating "the variable doesn't live here, its external. But it does exist, and it will be visible to you."
The variable should "live" in only one file.
If you try to make it "live" in multiple source-files (e.g. by declaring it in both b.c
and c.c
), you'll get the error that you saw.
Upvotes: 5