#include Directive

This page was translated by a robot.

Probably the most frequently used directive of the preprocessor. This directive explicitly inserts files at the specified position in the program code. The content of the inserted file is then also parsed. In the following example, a self-made header1.h file is inserted into another file with the #includedirective. Quotation and end marks are " "used for self-written files. The output of the second file shows what the preprocessor generates.



end of file 1

// This is file "header2.h"
// This is file "header1.h"
#define PI 3.14
// Including file finished.
// This is file "header1.h"
#define PI 3.14
---------------------------

// This is file "header2.h"
#include "header1.h"
// Including file finished.

With the #includedirective, the standard libraries are also inserted into the program code, but here the larger and smaller brackets <>are used:




Hello World!
#include <stdio.h>

int main(){
  printf("Hello World!\n");
  return 0;
}

Details

The greater and lesser brackets <>instruct the preprocessor to search for the specified file in specific search paths known by the system. This includes in particular the standard libraries as well as any libraries that may have been added to the system. However, the preprocessor does not search in the programmer's directory, so self-created files should not be included with greater and lesser brackets (trials have shown that the files are found anyway, but this is not recommended).

To include custom files using the #includedirective, the quote and trailing characters ""are required. These tell the preprocessor to first look in the current file's directory. If the file is not found, a search is made in the explicitly specified search path (e.g. also with a relative directory specification " ../mainheader.h"). If the file is still not found, a search is started again, which looks in the same places as if the file had been specified with greater and lesser brackets.

If the preprocessor does not find a file, it returns a No such file or directory error.

Distinction between header file and library

Linking using the #includedirective is often mistakenly referred to or understood as linking the associated library. In fact, however, the #includedirective only inserts a text file into the existing source code. This is usually a header file and serves as an interface (API) to the associated library.

The library itself, however, is usually available as a precompiled object file and is only connected to the compiled source code when it is linked. If the precompiled library cannot be found, a linker error will result. If the library is not statically linked into the finished program during linking, but is referenced as a dynamic library, it can happen that a program runs on one computer but not on another because the corresponding dynamic library is not found there can be. This is how the famous missing DLL error occurs.

In everyday programming, however, the consideration and distinction between static and dynamic libraries and their header files does not play such a major role. Accordingly, the term library is preferred to the term library header file on ManderC . For the sake of consistency, however, this is not used when specifying a corresponding header file .h, since this is no longer used under C++