Reputation: 83
I was reading the different phases of compilation process. When I reached Linking phase, I found that it links the object files of libraries and user defined into one to make it exe file. But preprocessing has already replaced the statement #include with its content (i.e, definition of functions like printf etc). I know that I am wrong somewhere but what have I understood incorrectly?
Upvotes: 1
Views: 136
Reputation: 21647
The library headers generally only declare the function and to not define it. If you look inside the .h files you will see something like (to use our example).
extern int printf (const char *fmt, ... ) ;
Although in many implementations these declarations are obscured by additional macros.
That declaration simply says there exists somewhere else a function called printf that that takes a points to character as an argument, followed by a variable number of unspecified arguments.
This allows the compiler to check to see whether the calls to printf in your code conform to what has been declared as the calling sequence for the function.
Unless you write printf yourself and include it in your code, there will be no printf when you compile your program.
When you link (or in some compilers, invoke the compiler in a manner that will automatically invoke the linker), the linker will search the specified libraries for one containing printf and include that with your code in the executable (ignoring shared libraries).
Upvotes: 1