Pragma once explained

In the C and C++ programming languages, '''#pragma once''' is a non-standard but widely supported preprocessor directive designed to cause the current header file to be included only once in a single compilation.[1] Thus, #pragma once serves the same purpose as include guards, but with several advantages, including less code, avoidance of name clashes, and sometimes improvement in compilation speed.[2] While #pragma once is available in most modern compilers, its implementation is tricky and might not always be reliable.

Example

File "grandparent.h"
  1. pragma once

struct foo

  • File "parent.h"
    1. include "grandparent.h"

    File "child.c"
    1. include "grandparent.h"
    2. include "parent.h"

    In this example, the inclusion of grandparent.h in both parent.h and child.c would ordinarily cause a compilation error, because a struct with a given name can only be defined a single time in a given compilation. The #pragma once directive serves to avoid this by ignoring subsequent inclusions of grandparent.h.

    Advantages

    Using #pragma once allows the C preprocessor to include a header file when it is needed and to ignore an #include directive otherwise. This has the effect of altering the behavior of the C preprocessor itself, and allows programmers to express file dependencies in a simple fashion, obviating the need for manual management.

    The most common alternative to #pragma once is to use #define to set an

    1. include guard
    macro, the name of which is picked by the programmer to be unique to that file. For example,

    1. ifndef GRANDPARENT_H
    2. define GRANDPARENT_H

    ... contents of grandparent.h

    1. endif /* !GRANDPARENT_H */

    This approach minimally ensures that the contents of the include file are not seen more than once. This is more verbose, requires greater manual intervention, and is prone to programmer error as there are no mechanisms available to the compiler for prevention of accidental use of the same macro name in more than one file, which would result in only one of the files being included. Such errors are unlikely to remain undetected but can complicate the interpretation of a compiler error report. Since the pre-processor itself is responsible for handling #pragma once, the programmer cannot make errors which cause name clashes.

    In the absence of

    1. include guards
    around #include directives, the use of #pragma once will improve compilation speed for some compilers since it is a higher-level mechanism; the compiler itself can compare filenames or inodes without having to invoke the C preprocessor to scan the header for #ifndef and #endif. Yet, since include guards appear very often and the overhead of opening files is significant, it is common for compilers to optimize the handling of include guards, making them as fast as #pragma once.[3] [4] [5]

    Caveats

    Identifying the same file on a file system is not a trivial task. Symbolic links and especially hard links may cause the same file to be found under different names in different directories. Compilers may use a heuristic that compares file size, modification time and content.[6] Additionally, #pragma once can do the wrong thing if the same file is intentionally copied into several parts of a project, e.g. when preparing the build. Whereas include guards would still protect from double definitions, #pragma once may or may not treat them as the same file in a compiler-dependent way. These difficulties, together with difficulties related to defining what constitutes the same file in the presence of hard links, networked file systems, etc. has so far prevented the standardization of #pragma once.

    The use of

    1. include guard
    macros allows dependent code to recognize and respond to slight differences in semantics or interfaces of competing alternatives. For example,

    1. include TLS_API_MACRO /* defined on the command line */

    ...

    1. if defined TLS_A_H

    ... use one known API

    1. elif defined TLS_B_H

    ... use another known API

    1. else
    2. error "unrecognized TLS API"
    3. endif

    In this case, the direct determination for which API is available would make use of the fact that the include file had advertised itself with its

    1. include guard
    macro.

    The #include directive is defined to represent a programmer's intention to actually include the text of a file at the point of the directive. This may occur several times within a single compilation unit, and is useful for evaluating macro-containing contents multiple times against changing definitions of the macro.

    The use of #pragma once, like the use of

    1. include guard
    macros within an include file places the responsibility upon its authors in order to protect against undesired multiple inclusion. Over-reliance upon either mechanism on the part of programmers by direct, unprotected use of #include directives without their own
    1. include guard
    will lead to failure when using an include file that has not protected itself with either mechanism.

    Portability

    CompilerSupport
    [7]
    [8]
    Cray C and C++ [9] (since 9.0)
    [10] (since XE3. classic compiler only.)
    [11]
    [12] (officially since 3.4[13] [14])
    [15] (since at least A.06.12)
    [16] (since 13.1.1)
    [17]
    [18] [19] (since 4.2)
    NVIDIA CUDA Compiler (depending on the underlying host compiler)
    [20]
    [21]
    [22]
    [23] (e.g. KEIL ARMCC 5)
    [24]
    [25] (since 12.5)
    [26] (since at least 17.4)
    [27] (since April 2015)
    [28] (as of April 2024)
    [29] (since v6.2r2)
    [30] (e.g. MSP430, ARM, C2000)

    External links

    Notes and References

    1. Web site: once . Microsoft Docs . 25 July 2019 . 3 November 2016.
    2. Web site: Games from Within: Even More Experiments with Includes . 2005-01-25 . 2013-08-19 . dead . https://web.archive.org/web/20080930061318/http://www.gamesfromwithin.com/articles/0501/000067.html . September 30, 2008 .
    3. Web site: The C Preprocessor: 1. The C Preprocessor . Gcc.gnu.org . 1996-02-01 . 2013-08-19.
    4. Web site: "Clang" CFE Internals Manual — Clang 3.4 documentation . Clang.llvm.org . 2013-08-19.
    5. Web site: clang: File manipulation routines . Clang.llvm.org . 2013-08-19.
    6. Web site: should_stack_file function in GCC source code.
    7. Web site: clang: clang: Pragma.cpp Source File . Clang.llvm.org . 2013-08-19 . dead . https://web.archive.org/web/20140404052351/http://clang.llvm.org/doxygen/Pragma_8cpp-source.html#l00184 . 2014-04-04 .
    8. Web site: Comeau C++ Pre-Release User Documentation: Pragmas . Comeaucomputing.com . 2013-08-19 . dead . 2013-12-11 . https://web.archive.org/web/20131211122659/http://www.comeaucomputing.com/4.0/docs/userman/pragma.html .
    9. Web site: CCE 9.0.0 Release Overview Introduction S-5212 . Cray Inc. . 2019-06-01 . 2019-09-23 .
    10. Web site: #pragma once - RAD Studio XE3 . Docwiki.embarcadero.com . 2010-12-02 . 2013-08-19.
    11. Web site: Pragmas . Digital Mars . 2013-08-19.
    12. Web site: Alternatives to Wrapper #ifndef . Gcc.gnu.org . 2013-08-20.
    13. Web site: GCC 3.4 Release Series — Changes, New Features, and Fixes . Gcc.gnu.org . 2013-08-19.
    14. Web site: GCC Bug 11569 - there's no substitute for #pragma once . 2003-07-18 . 2020-10-21.
    15. Web site: HP aC++/HP C A.06.29 Programmer's Guide; March 2016 (AR1603) .
    16. Web site: GCC pragmas . IBM . 2015-02-20.
    17. Web site: Diagnostic 1782: #pragma once is obsolete. Use #ifndef guard instead. . Intel Developer Zones . 4 December 2013 . dead . https://web.archive.org/web/20120131174333/http://software.intel.com/en-us/articles/cdiag1782/#comment-9466 . 31 January 2012 .
      1. pragma once should continue to work (Currently NOT deprecated) with the Intel Compiler.
      .
    18. Web site: once (C/C++) . Microsoft Developer Network . 2013-08-19 . https://web.archive.org/web/20160810031340/https://msdn.microsoft.com/en-us/library/4141z1cx(v=vs.71).aspx . 2016-08-10.
    19. Web site: once pragma Microsoft Docs.
    20. IDE help/documentation
    21. Web site: ARM Information Center . ARM . 2013-12-17.
    22. Web site: IAR C/C++ DevelopmentGuide . IAR SYSTEMS . 1 March 2023.
    23. Web site: Pragmas recognized by the compiler . Keil.
    24. Web site: #pragma once does not work if files referenced by alternate paths . "Now it should be fixed in git repository.".
    25. Web site: Oracle® Developer Studio 12.5: GCC Compatibility Guide . Oracle . 2016-07-26.
    26. Web site: The Portland Group . 31 July 2016.
    27. Web site: TinyCC pragma once implementation . 19 June 2018.
    28. Web site: SDCC Compiler User Guide section 3.16, page 62 . 11 April 2024.
    29. Web site: MA160-800 (v6.2r2) March 13, 2018 page 92.
    30. Web site: [EXT_EP-8185] Document #pragma once ]. Software Issue Report - Texas Instruments . Embedded Software & Tools . live . https://web.archive.org/web/20220129024318/https://sir.ext.ti.com/jira/browse/EXT_EP-8185 . Jan 29, 2022 .