Pragma once: Difference between revisions
→Portability: Order + Pelles C |
|||
Line 58: | Line 58: | ||
| [[Microsoft Visual C++]] || Supported<ref>http://msdn.microsoft.com/en-us/library/4141z1cx%28v=vs.71%29.aspx</ref> |
| [[Microsoft Visual C++]] || Supported<ref>http://msdn.microsoft.com/en-us/library/4141z1cx%28v=vs.71%29.aspx</ref> |
||
|- |
|- |
||
| [[Pelles C]] || Supported<ref>IDE help/documentation</ref |
| [[Pelles C]] || Supported<ref>IDE help/documentation</ref> |
||
|- |
|- |
||
|} |
|} |
Revision as of 09:29, 30 April 2013
This article needs additional citations for verification. (November 2011) |
This article is written like a manual or guide. (March 2013) |
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, #pragma once
serves the same purpose as #include
guards, but with several advantages, including: less code, avoiding name clashes, and sometimes improved compile speed.[1]
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"
Advantages and disadvantages
Using #pragma once
instead of include guards will for some compilers increase compilation speed 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
.
Common 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
.[2][3][4]
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 GRANDPARENT_H
in the Include guard article's example. This eliminates the risk of name clashes, meaning that no header file can fail to be included at least once.
GCC originally gave a warning declaring #pragma once
"obsolete" due to a problem with symbolic and hard links. However, this problem was fixed in GCC 3.4, and the feature was "un-deprecated" and the warning removed.[5] Furthermore, #pragma once
was removed from the list of obsolete features in GCC 4.4. [6]
Portability
Compiler | #pragma once |
---|---|
Clang | Supported[7] |
Comeau C/C++ | Supported[8] |
C++Builder XE3 | Supported[9] |
Digital Mars C++ | Supported[10] |
GCC | Supported[11] |
Intel C++ Compiler | Supported[12] |
Microsoft Visual C++ | Supported[13] |
Pelles C | Supported[14] |
Notes
- ^ http://web.archive.org/web/20080930061318/http://www.gamesfromwithin.com/articles/0501/000067.html
- ^ http://gcc.gnu.org/onlinedocs/gcc-2.95.3/cpp_1.html#SEC8
- ^ http://clang.llvm.org/docs/InternalsManual.html#MultipleIncludeOpt
- ^ http://clang.llvm.org/doxygen/group__CINDEX__FILES.html#ga1969fe907a40d9469ea68c370d0f602a
- ^ http://gcc.gnu.org/gcc-3.4/changes.html
- ^ http://gcc.gnu.org/onlinedocs/gcc-4.4.0/cpp/Obsolete-Features.html#Obsolete-Features
- ^ http://clang.llvm.org/doxygen/Pragma_8cpp-source.html#l00184
- ^ http://www.comeaucomputing.com/4.0/docs/userman/pragma.html
- ^ http://docwiki.embarcadero.com/RADStudio/XE3/en/Pragma_once
- ^ http://www.digitalmars.com/ctg/pragmas.html#once
- ^ http://gcc.gnu.org/onlinedocs/gcc-4.4.0/cpp/Alternatives-to-Wrapper-_0023ifndef.html
- ^ http://secure-software.intel.com/en-us/articles/cdiag1782/#comment-9466
- ^ http://msdn.microsoft.com/en-us/library/4141z1cx%28v=vs.71%29.aspx
- ^ IDE help/documentation