#pragma once
来源:互联网 发布:淘宝在哪里可以买彩票 编辑:程序博客网 时间:2024/04/30 15:19
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 in less code and without the possibility for name clashes.
See the article on include guards 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"
#pragma once struct foo { int member;};
- File "father.h"
#include "grandfather.h"
- File "child.c"
#include "grandfather.h"#include "father.h"
[edit] 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 inodes without having to invoke the C preprocessor to scan the header for #ifndef
and #endif
.
Some compilers such as GCC 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.[1]
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 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 cuts both ways; 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 links with different names point to the same file, then the compilation will fail. Compilers with #pragma once
-related bugs included LCC-Win32 as of 2004 [2][3] and GCC as of 1998.[4] 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.[5] [6]
You can use both #pragma once
and include guards to write portable code that can also take advantage of #pragma once
optimizations the compiler may support:
- File "grandfather.h"
#pragma once#ifndef GRANDFATHER_H#define GRANDFATHER_H struct foo { int member;}; #endif /* GRANDFATHER_H */
- #pragma once
- pragma once
- #pragma once
- #pragma once
- #pragma once
- pragma once
- #pragma once
- #pragma once
- #pragma once
- #pragma once
- #pragma once
- #pragma once
- #pragma once
- #pragma once
- #pragma once
- pragma once
- #pragma once
- #pragma once
- oaor
- Spring的bean 配置文件路径问题(转载)
- JBOSS配置数据源,以mysql为例
- 摄像头程序小知识
- session 失效时间 php
- #pragma once
- sql函数--DATEDIFF
- [信息采集专家]常用信息抓取的问题
- linux 下读写文件乱码 解决
- 办公积金的防被耍猴攻略
- [信息采集专家]如何抓取文章内容的同时将图片也下载到本地
- 学外语
- C# 格式化字符串 String.Format (推荐)
- 编译busybox