Yao Yao on March 14, 2015
• Published in category
• C++

A header file is a file containing the external declarations for a library.

The #include preprocessor directive tells the preprocessor to open the named header file and insert its contents where the #include statement appears.

#include <header> // search in some systematic paths
#include "local.h" // typically search relative to the current directory. If not found, reprocessed as <local.h>


The libraries that have been inherited from C are still available with the traditional ‘.h’ extension. However, you can also use them with the more modern C++ include style by prepending a “c” before the name. Thus:

#include <stdio.h>
#include <stdlib.h>


become:

#include <cstdio>
#include <cstdlib>


And so on, for all the Standard C headers. This provides a nice distinction to the reader indicating when you’re using C versus C++ libraries.

## How the linker searches a library

When you make an external reference to a function or variable in C or C++, the linker, upon encountering this reference, can do one of two things. If it has not already encountered the definition for the function or variable, it adds the identifier to its list of “unresolved references.” If the linker has already encountered the definition, the reference is resolved.

If the linker cannot find the definition in the list of object modules, it searches the libraries. Libraries have some sort of indexing so the linker doesn’t need to look through all the object modules in the library–-it just looks in the index. When the linker finds a definition in a library, the entire object module, not just the function definition, is linked into the executable program.

If you want to minimize executable program size, you might consider putting a single function in each source code file when you build your own libraries. This requires more editing3, but it can be helpful to the user.