Pragma once
Encyclopedia
In the C
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 guard
Include 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 guard
Include 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"
  1. pragma once


struct foo
{
int member;
};

File "father.h"
  1. include "grandfather.h"


File "child.c"
  1. include "grandfather.h"
  2. include "father.h"


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 inode
Inode
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 guard
Include 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 link
Symbolic 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"
  1. pragma once
  2. ifndef GRANDFATHER_H
  3. define GRANDFATHER_H


struct foo
{
int member;
};
  1. endif /* 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 MSDN
    Microsoft Developer Network
    The 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
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK