Why does GCC compile and link two files, even if "extern" is not used?

Below are two separate codes written in two separate files Test1.c and Test2.c. I do not use the keyword externin any file.

//Test1.c
#include <stdio.h>

int a = 1;
int main()
{
    printf("test1 - a val = %d\n",a);
    fn();
    printf("After Return : %d",a);
}

//Test2.c
#include <stdio.h>

int a;
int fn()
{
    printf("test2 - a val = %d\n",a);
    a++;
}

I compiled this code with gcc:

gcc Test1.c Test2.c

It generates the following output:

test1 - a val = 1
test2 - a val = 1

I tried printing the address of the variable ain both codes. The address is also the same.

Now I have the following questions:

  • Can it gccautomatically compile and link, even if externnot in use? Here, apparently, gccinternally does this when compiling these two files together.
  • Does this behavior with / without a externkeyword depend on the compiler?
+3
1

undefined . Test1.c Test2.c a , C11 6.9/5:

, , ( sizeof _Alignof, ), - ; .

. " " . (C11 6,9/4, 6,9/5). / " " " " " extern". static int x = 5; .


, GCC. 11 J.5.11 " ":

extern ; , undefined.

gcc , , . , " " , .

+3

Source: https://habr.com/ru/post/1669648/


All Articles