Developers often split libraries into two kinds of files:
- header files
- implementation files
A header file defines an interface of a library — which functions, global variables, etc are available to use by the library clients (i.e., code that uses tools from the library).
Header files typically end with a
.h extension (e.g.,
cs50.h) and are included into your program with the
#include preprocessor directive.
Implementation files contain the implementation details (e.g., function definitions).
Both header files and implementation files are compiled together and produce the library's object file (the one that contains the 0s and 1s of the library).
Lastly, when you link a library's object file to your program, the object code of the library gets linked with the object code of your program to produce the final executable file via which you execute your program.
Watch this for more info!
And does #include paste in the whole header file or just the relevant
The whole file. Compile your program with the
-E option to see the output of the preprocessor.
does linking the library paste the whole library (all those function
definitions) or just the relevant information (the definitions of the
functions you used)?
All I can say at the mean time is that the linking process is a bit more complicated than that. When the compiler doesn't find a definition of referenced function, it just assumes that it is defined in another file leaving that for the linker to resolve. So you may think of the linker as a program that resolves these undefined references by linking them to their already compiled definitions.
Frankly, I have no detailed info whether the whole object code of the library is used in this process or just the code for the referenced functions (hopefully I'll update this answer once I learn more) but the knowledge that you should get from this answer should probably be enough for you to proceed for now.