Suppose the following code:
a.c:
#include <stdio.h>
int a;
int func();
int main(int argc, char **argv) {
a = 7;
int a2 = func();
printf("a is %d, a2 is %d\n", a, a2);
return 0;
}
and b.c:
int a;
int func()
{
a = 9;
return a;
}
When compiled with g++ a.c b.c -Wall -O0 it produces a linking error, as expected. However, when invoking gcc a.c b.c -Wall -O0 it produces no warning and no error!
The output is a is 9, a2 is 9 by the way.
gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4)
Why does GCC allow this? I was surprised by this behaviour. If you initialize the variables at declaration, then linking will also fail with GCC.