Pragma once
Encyclopedia
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. Thus,
s, but with several advantages, including: less code, avoiding name clashes, and sometimes improved compile speed.
See the article on
s for an example of a situation in which one or the other of these methods must be used. The solution using include guards is given on that page; the
File "grandfather.h"
File "father.h"
File "child.c"
s without having to invoke the C preprocessor
to scan the header for
Some compilers such as GCC
, Clang
, and EDG-based compilers
include special speedup code to recognize and optimize the handling of include guards, and thus little or no speedup benefit is obtained from the use of #pragma once.
Again because the compiler itself is responsible for handling
article's example. This eliminates the risk of name clashes, meaning that no header file can fail to be included at least once. It also requires less typing than the include guard method.
However, this high-level handling is not perfect; the programmer must rely on the compiler to handle
s with different names point to the same file, then the compilation will fail. Compilers with
GCC originally gave a warning declaring
Both
File "grandfather.h"
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....
and C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...
programming languages, #pragma once is a non-standard but widely supported preprocessor directive
C preprocessor
The C preprocessor is the preprocessor for the C and C++ computer programming languages. The preprocessor handles directives for source file inclusion , macro definitions , and conditional inclusion ....
designed to cause the current source file to be included only once in a single compilation. Thus,
#pragma once
serves the same purpose as #include
guardInclude guard
In the C and C++ programming languages, an #include guard, sometimes called a macro guard, is a particular construct used to avoid the problem of double inclusion when dealing with the #include directive...
s, but with several advantages, including: less code, avoiding name clashes, and sometimes improved compile speed.
See the article on
#include
guardInclude guard
In the C and C++ programming languages, an #include guard, sometimes called a macro guard, is a particular construct used to avoid the problem of double inclusion when dealing with the #include directive...
s for an example of a situation in which one or the other of these methods must be used. The solution using include guards is given on that page; the
#pragma once
solution would be:File "grandfather.h"
File "father.h"
File "child.c"
Advantages and disadvantages
Using#pragma once
instead of include guards will typically increase compilation speed since it is a higher-level mechanism; the compiler itself can compare filenames or inodeInode
In computing, an inode is a data structure on a traditional Unix-style file system such as UFS. An inode stores all the information about a regular file, directory, or other file system object, except its data and name....
s without having to invoke the C preprocessor
C preprocessor
The C preprocessor is the preprocessor for the C and C++ computer programming languages. The preprocessor handles directives for source file inclusion , macro definitions , and conditional inclusion ....
to scan the header for
#ifndef
and #endif
.Some compilers such as GCC
GNU Compiler Collection
The GNU Compiler Collection is a compiler system produced by the GNU Project supporting various programming languages. GCC is a key component of the GNU toolchain...
, Clang
Clang
Clang is a compiler front end for the C, C++, Objective-C, and Objective-C++ programming languages. It uses the Low Level Virtual Machine as its back end, and Clang has been part of LLVM releases since LLVM 2.6....
, and EDG-based compilers
Edison Design Group
The Edison Design Group is a company that makes compiler frontends . Their frontends are widely used in commercially available compilers and code analysis tools. Users include the Intel C++ compiler, SGI MIPSpro, The Portland Group, and Comeau C++...
include special speedup code to recognize and optimize the handling of include guards, and thus little or no speedup benefit is obtained from the use of #pragma once.
Again because the compiler itself is responsible for handling
#pragma once
, it is not necessary for the programmer to create new macro names such as GRANDFATHER_H
in the Include guardInclude guard
In the C and C++ programming languages, an #include guard, sometimes called a macro guard, is a particular construct used to avoid the problem of double inclusion when dealing with the #include directive...
article's example. This eliminates the risk of name clashes, meaning that no header file can fail to be included at least once. It also requires less typing than the include guard method.
However, this high-level handling is not perfect; the programmer must rely on the compiler to handle
#pragma once
correctly. If the compiler makes a mistake, for example by failing to recognize that two symbolic linkSymbolic link
In computing, a symbolic link is a special type of file that contains a reference to another file or directory in the form of an absolute or relative path and that affects pathname resolution. Symbolic links were already present by 1978 in mini-computer operating systems from DEC and Data...
s with different names point to the same file, then the compilation will fail. Compilers with
#pragma once
-related bugs included LCC-Win32 and GCC .GCC originally gave a warning declaring
#pragma once
"obsolete" when compiling code that used it. However, with the 3.4 release of GCC, the #pragma once
handling code was fixed to behave correctly with symbolic and hard links. The feature was "un-deprecated" and the warning removed.Both
#pragma once
and include guards can be used to write portable code that can also take advantage of #pragma once
optimizations the compiler may support:File "grandfather.h"
Portability
Compiler | #pragma once |
---|---|
Clang Clang Clang is a compiler front end for the C, C++, Objective-C, and Objective-C++ programming languages. It uses the Low Level Virtual Machine as its back end, and Clang has been part of LLVM releases since LLVM 2.6.... |
? |
Comeau C/C++ Comeau C/C++ Comeau C/C++ is a C and C++ compiler produced by Comeau Computing.The compiler supports several dialects of both the C and C++ languages... |
Supported |
Digital Mars C++ | Supported |
GCC GNU Compiler Collection The GNU Compiler Collection is a compiler system produced by the GNU Project supporting various programming languages. GCC is a key component of the GNU toolchain... |
Supported |
Intel C++ Compiler Intel C++ Compiler Intel C++ Compiler is a group of C and C++ compilers from Intel Corporation available for GNU/Linux, Mac OS X, and Microsoft Windows.... |
Supported |
Microsoft Visual Studio Microsoft Visual Studio Microsoft Visual Studio is an integrated development environment from Microsoft. It is used to develop console and graphical user interface applications along with Windows Forms applications, web sites, web applications, and web services in both native code together with managed code for all... |
Supported |
External links
- "Pragma Directives (C/C++)" at MSDNMicrosoft Developer NetworkThe Microsoft Developer Network is the portion of Microsoft responsible for managing the firm's relationship with developers and testers: hardware developers interested in the operating system , developers standing on the various OS platforms, developers using the API and scripting languages of...
- GCC Pragma and other commands at GNU Org