I read a few posts and concluded that extern tells compiler that “This function exists, but the code for it is somewhere else. Don’t panic.” But how does the linker know where the function is defined.
My CASE:-
I am working on Keil uvision 4. There is a header file grlib.h and the main function is in grlib_demo.c(it includes grlib.h). Now, there is a function GrCircleDraw() which is defined in Circle.c and called in grlib_demo.c, there is also a statement
extern void GrCircleDraw(all arguments);
in grlib.h. My query is how linker knows where the definition of GrCircleDraw() is since Circle.c is not included in grlib.h and grlib_demo.c
Note :- The files grlib.h and Circle.c are in the same folder. The code runs successfully.
The simple answer is that “the compiler doesn’t need to know, but the linker has to be able to find it”. Through multiple
.ofiles, or through libraries, the linker has to be able to find a single definition of theGrCircleDrawfunction.