C++: Include a File


The task is to demonstrate the language’s ability to include source code from other files.

In C and C++, inclusion of other files is achieved via a preprocessor. The #include preprocessor directive tells the compiler to incorporate code from the included file. This is normally used near the top of a source file and is usually used to tell the compiler to include header files for the function libraries.

/* Standard and other library header names are enclosed between chevrons */
#include <stdlib.h>
/* User/in-project header names are usually enclosed between double-quotes */
#include "myutil.h"

Although it is often conventional and idiomatic for a project to use its own headers in the style described on the second line above, it’s also possible to tell most compilers using various flags (e. g. GCC and Clang accept -I) to treat an arbitrary directory as a system/library include folder, thereby allowing any contained files to be included using the angle bracket syntax.


Content is available under GNU Free Documentation License 1.2.