Reputation: 1050
I have a header header.hxx like this:
// header.hxx
#ifndef HEADER1
#define HEADER1
typedef struct s_ringreader{
// some stuff in header
} RingReader;
RingReader defaultReader;
#endif
and multiple cxx like this, source1.cxx
// source1.cxx
# include "header.hxx"
void fun1(){
// some stuff using defaultReader
}
, source2.cxx
// source2.cxx
# include "header.hxx"
void fun2(){
// some other stuff using defaultReader
}
and source3.cxx
// source3.cxx
# include "header.hxx"
void fun3(){
// some other stuff using defaultReader
}
My question is, should I not be getting mutliple definition errors, as I am including the header three times? Theoritically, in order for not getting this error I shoul use either static
or extern
for RingReader defaultReader
but this is not happening.
As a note, this happens with some version of arm-poky compiler, but not with a different version. Is there any cases (like a different compilier) where these situations are not treated like errors?
Upvotes: 1
Views: 103
Reputation: 39818
Since this code is valid C as well as C++, certain compilers will as an extension (C11 §J.5.11) interpret uninitialized global variables as common symbols. These act like C++17 inline variables and are merged at link time. This interpretation (like any other whatsoever!) is conforming for a C++ implementation since the program is ill-formed, no diagnostic required.
Upvotes: 0