Source file inclusion

From cppreference.com
 
 
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements (loops)
Jump statements
Functions
Function declaration
Lambda function declaration
inline specifier
Exception specifications (deprecated)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
decltype (C++11)
auto (C++11)
alignas (C++11)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Implicit conversions - Explicit conversions
static_cast - dynamic_cast
const_cast - reinterpret_cast
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
Miscellaneous
 
 

Includes other source file into current source file at the line immediately after the directive.

Syntax

#include <filename> (1)
#include "filename" (2)
__has_include ( " filename " )
__has_include ( < filename > )
(3) (since C++17)

Any preprocessing tokens (macro constants or expressions) are permitted as arguments to #include and __has_include (since C++17) as long as they expand to a sequence of characters surrounded by < > or " ".

Explanation

1,2) Includes source file, identified by filename into the current source file at the line immediately after the directive. In the case the file is not found, program is ill-formed.
1) Searches for the file in implementation-defined manner. The intent of this syntax is to search for the files under control of the implementation. Typical implementations search only standard include directories. The standard C++ library and the standard C library are implicitly included in these standard include directories. The standard include directories usually can be controlled by the user through compiler options.
2) Searches for the file in implementation-defined manner. The intent of this syntax is to search for the files that are not controlled by the implementation. Typical implementations first search the directory where the current file resides and, only if the file is not found, search the standard include directories as with (1).
3) Preprocessor constant expression that evaluates to 1 if the file name is found and 0 if not. The program is ill-formed if the argument would not be a valid argument to the #include directive.

Notes

When a file is included, it is processed by translation phases 1-4, which may include, recursively, expansion of the nested #include directives. To avoid repeated inclusion of the same file and endless recursion when a file includes itself, perhaps transitively, header guards are commonly used: the entire header is wrapped in

#ifndef FOO_H_INCLUDED /* any name uniquely mapped to file name */
#define FOO_H_INCLUDED
// contents of the file are here
#endif

Many compilers also implement the non-standard pragma #pragma once with similar effects: it disables processing of a file if the same file (where file identity is determined in OS-specific way) has already been included.

Example

#if __has_include(<optional>)
#  include <optional>
#  define have_optional 1
#elif __has_include(<experimental/optional>)
#  include <experimental/optional>
#  define have_optional 1
#  define experimental_optional 1
#else
#  define have_optional 0
#endif
 
#ifndef TEXT
    #include <iostream>
 
    int main()
    {
    #define TEXT "Hello, world!"
    #include __FILE__
    #define TEXT "Hello again!"
    #include __FILE__
 
    }
#else
    std::cout << TEXT << '\n';
    #undef TEXT
#endif

Output:

Hello, world!
Hello again!

See also

cpp/header a list of C++ Standard Library header files
C documentation for Source file inclusion