Are C libraries linked with object code or first with source code so only later with object code? I mean, look at the image found at Cardiff School of Computer Science & Informatics's website :
It's "strange" that after generating object-code the libraries are being linked. I mean, we use the source code while putting the includes!
So.. How this actually works? Thanks!
That diagram is correct.
When you
#include
a header, it essentially copies that header into your file. A header is a list of types and function declarations and constants, etc., but doesn't contain any actual code (C++ and inline functions notwithstanding).Let's have an example: library.h
library.c
yourcode.c
When you
#include
library.h, you get the declaration offoo()
. The compiler, at this point, knows nothing else about foo() or what it does. The compiler can freely insert calls tofoo()
despite this. The linker, seeing a call tofoo()
in youcode.c, and seeing the code in library.c, knows that any calls tofoo()
should go to that code.In other words, the compiler tells the linker what function to call, and the linker (given all the object code) knows where that function actually is.
(Thanks to Fiddling Bits for corrections.)