multiple definition and static keyword

74 Views Asked by At

the function f in 2.o have global scope, and 1.c if he wanted he could use it. but in 1.o have unique definition of f. the linker agree to accept it though How is there no conflict here and which definition does he choose and why ? Doesn't the function f in file 2.c have to be static as well ?

1.c
static void f() {
}
gcc -c 1.c 

2.c 
void f() {
}
gcc -c 1.c

gcc 1.o 2.o -myprog
1

There are 1 best solutions below

0
k314159 On

There is no problem here. Function f in 2.c is globally visible. Function f in 1.c is only visible inside that translation unit, i.e. only visible to code inside 1.c. The function f in 2.c is not known to any part of 1.c because you didn't declare it as an extern function, so any code in 1.c that references (or calls) f will use the f defined in 1.c.