I understand that extern is the default storage-class specifier for objects defined at file scope
That's true but the linkage breaks because of "redefinition" of the gv
symbol, isn't it?
That's because both test.c
and main.c
have int gv;
after the preprocessor includes the headers. Thus eventually both objects test.o
and main.o
contain _gv
symbol.
The most common solution is to have extern int gv;
in the test.h
header file (which tells the compiler that gv
storage is allocated somewhere else). And inside the C file, main.c
for example, define int gv;
so that the storage for gv
will be actually allocated but only once, inside main.o
object.
EDIT:
Referring the same link you provided storage-class specifier, which contains the following statement:
Declarations with external linkage are commonly made available in header files so that all translation units that #include the file may refer to the same identifier that are defined elsewhere.