Name

#include directive — Includes another source file

Synopsis

#include <header>
#include "sourcefile"

The #include directive includes the contents of a standard header or source file. The first form searches for header and replaces the directive with the entire contents of the header. The second form searches for sourcefile and replaces the directive with the entire contents of the named source file.

The basic action of #include is to read the named file or header as though its entire contents appeared in the source file at the position of the #include directive. Typically, common declarations are placed in a separate file, such as decl.h, and #include "decl.h" is used in every source file that depends on those declarations.

image with no caption

If a source file contains the directive #include "filename“, and the compiler cannot find the external file named filename, the compiler also tries the form #include < filename >. Most compilers implement these two forms of #include by searching in different folders or directories. For example, the quote form searches in the current directory or in the directory that contains the source file, and the angle-bracket form searches only in “system” directories. Such details are implementation-defined, and some compilers might introduce further distinctions between the two forms.

It is possible, for example, for a compiler to recognize only the standard headers in the ...

Get C++ In a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.