C Header files and Computation/Linking
So I know that header files have forward declarations of various
functions, structs, etc. that are used in the .c file that 'calls' the
#include, right? As far as I understand, the "separation of powers" occurs
like this:
Header file: func.h
contains forward declaration of function
int func(int i);
C source file: func.c
contains actual function definition
#include "func.h"
int func(int i) {
return ++i ;
}
C source 2 ("actual" program):
#include <stdio.h>
#include "func.h"
int main(void) {
int res = func(3);
printf("%i", res);
}
My question is: being that #include is simply a compiler directive that
copies the contents of the .h in the file that #include is in, how does
the .c file know how to actually excecute the function? All it's getting
is the < int func(int i); >, so how can it actually perform the function?
How does it gain access to the actual definition of func? Does the header
include some sort of 'pointer' that says "that's my definition, over
there! "?
How does it work?
Does it have something to do with how you compile it?
P.S Sorry, not able to do code snippets properly, posting from mobile.
No comments:
Post a Comment