Reputation: 21
I declared a global static function in one file
a.c
static void Func1(void);
void Func2(void);
void Func1(void) {
puts("Func1 Called");
}
void Func2(void) {
puts("Func2 Called");
}
and accessed it in b.c
#include <stdio.h>
#include "a.c"
void main() {
Func1();
Func2();
}
the program complied successfully, but as per provided information this should give error: undefined reference to Func1
. What wrong is happening here?
Upvotes: 2
Views: 185
Reputation: 158
You declare in header
files and define in .c
files. So you must use header files to represent the variables or functions you defined. Instead if you use .c
files then it leads to multiple definitions.I think that is why you can access that global function.
Upvotes: 0
Reputation: 134286
You don't include a source file in another, you compile and link them together.
In you case, by saying #include "a.c"
, you're essentially putting the whole content of a.c
into b.c
and then starting the compilation, so the static
functions and their calls are present in the same translation unit. Thus, there is no issue for compiler to find the called function.
Instead, if you do something like
gcc a.c b.c -o a.out //try to compile and link together
you'll see the expected error, as in this case, a.c
and b.c
will be two separate translation units.
Upvotes: 2