C and C++ do not require a program to be broken into
multiple files, but it is often helpful to do so.
These files may be compiled separately, which gives the
main reasons to use them:
- To create libraries, where the contents of the library is compiled
once and stored, and various programs use its facilities.
- To reduce the time to recompile a large program. Large programs are
often broken into several files so that when the system is
rebuilt after a change, only the files which changed need to be
- A file can be used as a unit for data-hiding purposes. This is
especially important in plain C, since it does not have classes.
A multi-file program contains several code files (.c or .cpp) which
contain the various executable code, and header (.h) files which are used
to relate them. The code files use the #include directive
to read the headers. The code files are compiled
and combined by a link step. The header files are not compiled,
except indirectly when the #include directive is performed.