c++header-filesinclude-guards

What include guards exist in system/standard library headers, if any?


I am wondering if/ what include guards exist in files like windows.h, math.h, iostream, stdio... etc.

Since I have those headers included multiple times in different files. Do those files already have guards built in or is there a definition defined?

I am just wondering what the standards are for that kind of thing.


Solution

  • If you open the file to read the contents (you can even right click the include directive in most editors to open the file), you will see that include files usually start with something like:

    #ifndef _WINDOWS_
    #define _WINDOWS_
    ...
    

    So the first time it will go in the file since _WINDOWS_ is not defined, therefore it will be defined and the contents of the file will be included. The second time the #ifndef will fail since the define was done previously.

    This is the standard way to put a safeguard. Aanother way, which is supported by many compilers, is to use #pragma once. This has the advantage of preventing collisions even if someone defines the same constant in another header file.