pragma once

In the C and C++ programming languages, #pragma once is a non-standard but widely supported preprocessor directive designed to cause the current source 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]

Example

File "grandparent.h"
#pragma once

struct foo 
{
    int member;
};
File "parent.h"
#include "grandparent.h"
File "child.c"
#include "grandparent.h"
#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 frees programmers to express file dependencies in a simple fashion, relieving them of the burden and tedium of manual management.

The most common alternative to #pragma once is to use #define to set an #include guard macro, the name of which is picked by the programmer to be unique to that file. For example,

#ifndef GRANDPARENT_H
#define GRANDPARENT_H
... contents of grandparent.h
#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 #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.[6] 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.[7] 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 #include guard macros allows dependent code to recognize and respond to slight differences in semantics or interfaces of competing alternatives. For example,

#include TLS_API_MACRO /* defined on the command line */

...

#if defined TLS_A_H
... use one known API
#elif defined TLS_B_H
... use another known API
#else
#error "unrecognized TLS API"
#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 #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 #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 #include guard will lead to failure when using an include file that has not protected itself with either mechanism.

Portability

Compiler #pragma once
ClangSupported[8]
Comeau C/C++Supported[9]
Cray C and C++Supported[10] (since 9.0)
C++Builder XE3Supported[11]
Digital Mars C++Supported[12]
GCCSupported[13] (since 3.4[6])
HP C/aC++Supported[14] (since at least A.06.12)
IBM XL C/C++Supported[15] (since 13.1.1)
Intel C++ CompilerSupported[16]
Microsoft Visual C++Supported[17][18] (since 4.2)
NVIDIA CUDA Compiler Supported (depending on the underlying host compiler)
Pelles CSupported[19]
ARM DS-5Supported[20]
IAR C/C++Supported[21]
Keil CC 5Supported[22]
Oracle Developer Studio C/C++Supported[23] (since 12.5)
Portland Group C/C++Supported[24] (since at least 17.4)
TinyCCSupported[25] (since April 2015)
TASKING VX-toolset for TriCore: C compilerSupported[26] (since v6.2r2)
gollark: I get CHANINFOs for each channel.
gollark: How does my server get sent this information? This implies that I can't just make modes be a `set[Mode]`, which is *irritating*.
gollark: We are doing things with APIONET.
gollark: Internet IRC relay chat.
gollark: Not all of them? OH BEE.

References

  1. "once". Microsoft Docs. 3 November 2016. Retrieved 25 July 2019.
  2. "Games from Within: Even More Experiments with Includes". Web.archive.org. 2005-01-25. Archived from the original on September 30, 2008. Retrieved 2013-08-19.
  3. "The C Preprocessor: 1. The C Preprocessor". Gcc.gnu.org. 1996-02-01. Retrieved 2013-08-19.
  4. ""Clang" CFE Internals Manual — Clang 3.4 documentation". Clang.llvm.org. Retrieved 2013-08-19.
  5. "clang: File manipulation routines". Clang.llvm.org. Retrieved 2013-08-19.
  6. "GCC 3.4 Release Series — Changes, New Features, and Fixes". Gcc.gnu.org. Retrieved 2013-08-19.
  7. "should_stack_file() function in GCC source code".
  8. "clang: clang: Pragma.cpp Source File". Clang.llvm.org. Archived from the original on 2014-04-04. Retrieved 2013-08-19.
  9. "Comeau C++ Pre-Release User Documentation: Pragmas". Comeaucomputing.com. Retrieved 2013-08-19.
  10. "CCE 9.0.0 Release Overview Introduction S-5212". Cray Inc. 2019-06-01. Retrieved 2019-09-23.
  11. "#pragma once - RAD Studio XE3". Docwiki.embarcadero.com. 2010-12-02. Retrieved 2013-08-19.
  12. "Pragmas". Digital Mars. Retrieved 2013-08-19.
  13. "Alternatives to Wrapper #ifndef". Gcc.gnu.org. Retrieved 2013-08-20.
  14. "HP aC++/HP C A.06.29 Programmer's Guide; March 2016 (AR1603)".
  15. "Supported GCC pragmas". IBM. Retrieved 2015-02-20.
  16. "Diagnostic 1782: #pragma once is obsolete. Use #ifndef guard instead". Intel Developer Zones. Retrieved 4 December 2013.
  17. "once (C/C++)". Msdn.microsoft.com. Archived from the original on 2016-08-10. Retrieved 2013-08-19.
  18. https://msdn.microsoft.com/en-us/library/4141z1cx.aspx
  19. IDE help/documentation
  20. "ARM Information Center". ARM. Retrieved 2013-12-17.
  21. "IAR C/C++ Development Guide" (PDF). IAR Systems. Archived from the original (PDF) on 16 May 2017. Retrieved 4 December 2013.
  22. "Pragmas recognized by the compiler". Keil.
  23. "Oracle® Developer Studio 12.5: GCC Compatibility Guide". Oracle. Retrieved 2016-07-26.
  24. "The Portland Group". Retrieved 31 July 2016.
  25. "TinyCC pragma once implementation". Retrieved 19 June 2018.
  26. "MA160-800 (v6.2r2) March 13, 2018 page 92" (PDF).
This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.