For faster navigation, this Iframe is preloading the Wikiwand page for #pragma once.

#pragma once

CC++程式語言中,#pragma once是一個非標準但是被廣泛支援的前置處理符號,會讓所在的檔案在一個單獨的編譯中只被包含一次。以此方式,#pragma once提供類似include防範的目的,但是擁有較少的程式碼且能避免名稱的碰撞。

示例

[编辑]

請參考include防範裡其中一種狀況的範例或其他的使用方法。如下:

grandparent.h
#pragma once

struct foo 
{
    int member;
};
parent.h
#include "grandparent.h"
child.c
#include "grandparent.h"
#include "parent.h"

優缺點

[编辑]

使用#pragma once代替include防範將加快編譯速度,因為這是一種高階的機制;編譯器會自動比對檔案名稱或inode而不需要在標頭檔去判斷#ifndef#endif

另一方面,部份編譯器,例如GCCclang等,也包含特別的程式碼來識別和有效率的管理include防範。因此使用#pragma once并不会得到明显的加速。[1][2][3]

此外,因為編譯器自己必須承擔管理#pragma once,它不必定義新的指令名稱,例如在include防範文章範例的H_GRANDFATHER。這能排除名稱碰撞的風險,意思就是至少第一次包含標頭檔不會再有錯誤

然而,這種高階的管理有好也有壞;設計者必須依賴編譯器正確的管理#pragma once。編譯器如果犯錯,例如沒有辨認出在相同檔案中的兩個不同符號連結名稱指標,此時編譯會錯誤。編譯器對於#pragma once可能包含相關的bug LCC-Win32 2004年[1][2]和GCC 1998年。[3]页面存档备份,存于互联网档案馆)2005年,GCC文件中將#pragma once列為「已淘汰」的特性。[4]页面存档备份,存于互联网档案馆)随着gcc 3.4的发布,gcc解决了#pragma once中的一些问题(主要是跟符号链接和硬链接有关),并且去掉了#pragma once的“已淘汰”的标签。[5]页面存档备份,存于互联网档案馆

编译器支持

[编辑]
Compiler #pragma once
Clang 支持[4]
Comeau C/C++英语Comeau C/C++ 支持[5]
C++Builder XE Seattle 支持[6]
Digital Mars C++ 支持[7]
GCC 支持[8](3.4版本以后[9]
Intel C++編譯器 支持[10]
Microsoft Visual C++ 支持[11]
Pelles C 支持[12]
IAR C/C++ 支持[13] (2022年以後[14])
TinyCC 支持[15] (2015年以後)

参考文献

[编辑]
  1. ^ The C Preprocessor: 1. The C Preprocessor. Gcc.gnu.org. 1996-02-01 [2013-08-19]. (原始内容存档于2013-08-07). 
  2. ^ “Clang”CFE Internals Manual—Clang 3.4 documentation. Clang.llvm.org. [2013-08-19]. (原始内容存档于2013-08-06). 
  3. ^ clang: File manipulation routines. Clang.llvm.org. [2013-08-19]. (原始内容存档于2013-11-11). 
  4. ^ clang: clang: Pragma.cpp Source File. Clang.llvm.org. [2013-08-19]. (原始内容存档于2014-04-04). 
  5. ^ Comeau C++ Pre-Release User Documentation: Pragmas. Comeaucomputing.com. [2013-08-19]. (原始内容存档于2013-12-11). 
  6. ^ #pragma once - RAD Studio XE Seattle. Docwiki.embarcadero.com. 2015-08-25 [2016-03-10]. (原始内容存档于2016-03-10). 
  7. ^ Pragmas. Digital Mars. [2013-08-19]. (原始内容存档于2013-12-12). 
  8. ^ Alternatives to Wrapper #ifndef. Gcc.gnu.org. [2013-08-20]. (原始内容存档于2016-03-04). 
  9. ^ GCC 3.4 Release Series—Changes, New Features, and Fixes. Gcc.gnu.org. [2013-08-19]. (原始内容存档于2013-08-07). 
  10. ^ Diagnostic 1782: #pragma once is obsolete. Use #ifndef guard instead.. Intel Developer Zones. [4 December 2013]. (原始内容存档于2013-12-11). 
  11. ^ once(C/C++). Msdn.microsoft.com. [2013-08-19]. (原始内容存档于2016-08-10). 
  12. ^ IDE help/documentation
  13. ^ IAR C/C++ Development Guide (PDF). IAR Systems. [4 December 2013]. (原始内容 (PDF)存档于2017年5月16日). 
  14. ^ Supported but not yet documented (doc planned for spring 2022)
  15. ^ TinyCC pragma once implementation. [19 June 2018]. (原始内容存档于2021-12-07). 

外部連結

[编辑]
{{bottomLinkPreText}} {{bottomLinkText}}
#pragma once
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?