1<html lang="en"> 2<head> 3<title>Alternatives to Wrapper #ifndef - The C Preprocessor</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="The C Preprocessor"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="Header-Files.html#Header-Files" title="Header Files"> 9<link rel="prev" href="Once_002dOnly-Headers.html#Once_002dOnly-Headers" title="Once-Only Headers"> 10<link rel="next" href="Computed-Includes.html#Computed-Includes" title="Computed Includes"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<!-- 13Copyright (C) 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 141997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 152008, 2009, 2010, 2011 16Free Software Foundation, Inc. 17 18Permission is granted to copy, distribute and/or modify this document 19under the terms of the GNU Free Documentation License, Version 1.3 or 20any later version published by the Free Software Foundation. A copy of 21the license is included in the 22section entitled ``GNU Free Documentation License''. 23 24This manual contains no Invariant Sections. The Front-Cover Texts are 25(a) (see below), and the Back-Cover Texts are (b) (see below). 26 27(a) The FSF's Front-Cover Text is: 28 29 A GNU Manual 30 31(b) The FSF's Back-Cover Text is: 32 33 You have freedom to copy and modify this GNU Manual, like GNU 34 software. Copies published by the Free Software Foundation raise 35 funds for GNU development. 36--> 37<meta http-equiv="Content-Style-Type" content="text/css"> 38<style type="text/css"><!-- 39 pre.display { font-family:inherit } 40 pre.format { font-family:inherit } 41 pre.smalldisplay { font-family:inherit; font-size:smaller } 42 pre.smallformat { font-family:inherit; font-size:smaller } 43 pre.smallexample { font-size:smaller } 44 pre.smalllisp { font-size:smaller } 45 span.sc { font-variant:small-caps } 46 span.roman { font-family:serif; font-weight:normal; } 47 span.sansserif { font-family:sans-serif; font-weight:normal; } 48--></style> 49<link rel="stylesheet" type="text/css" href="../cs.css"> 50</head> 51<body> 52<div class="node"> 53<a name="Alternatives-to-Wrapper-%23ifndef"></a> 54<a name="Alternatives-to-Wrapper-_0023ifndef"></a> 55<p> 56Next: <a rel="next" accesskey="n" href="Computed-Includes.html#Computed-Includes">Computed Includes</a>, 57Previous: <a rel="previous" accesskey="p" href="Once_002dOnly-Headers.html#Once_002dOnly-Headers">Once-Only Headers</a>, 58Up: <a rel="up" accesskey="u" href="Header-Files.html#Header-Files">Header Files</a> 59<hr> 60</div> 61 62<h3 class="section">2.5 Alternatives to Wrapper #ifndef</h3> 63 64<p>CPP supports two more ways of indicating that a header file should be 65read only once. Neither one is as portable as a wrapper ‘<samp><span class="samp">#ifndef</span></samp>’ 66and we recommend you do not use them in new programs, with the caveat 67that ‘<samp><span class="samp">#import</span></samp>’ is standard practice in Objective-C. 68 69 <p><a name="index-g_t_0023import-33"></a>CPP supports a variant of ‘<samp><span class="samp">#include</span></samp>’ called ‘<samp><span class="samp">#import</span></samp>’ which 70includes a file, but does so at most once. If you use ‘<samp><span class="samp">#import</span></samp>’ 71instead of ‘<samp><span class="samp">#include</span></samp>’, then you don't need the conditionals 72inside the header file to prevent multiple inclusion of the contents. 73‘<samp><span class="samp">#import</span></samp>’ is standard in Objective-C, but is considered a 74deprecated extension in C and C++. 75 76 <p>‘<samp><span class="samp">#import</span></samp>’ is not a well designed feature. It requires the users of 77a header file to know that it should only be included once. It is much 78better for the header file's implementor to write the file so that users 79don't need to know this. Using a wrapper ‘<samp><span class="samp">#ifndef</span></samp>’ accomplishes 80this goal. 81 82 <p>In the present implementation, a single use of ‘<samp><span class="samp">#import</span></samp>’ will 83prevent the file from ever being read again, by either ‘<samp><span class="samp">#import</span></samp>’ or 84‘<samp><span class="samp">#include</span></samp>’. You should not rely on this; do not use both 85‘<samp><span class="samp">#import</span></samp>’ and ‘<samp><span class="samp">#include</span></samp>’ to refer to the same header file. 86 87 <p>Another way to prevent a header file from being included more than once 88is with the ‘<samp><span class="samp">#pragma once</span></samp>’ directive. If ‘<samp><span class="samp">#pragma once</span></samp>’ is 89seen when scanning a header file, that file will never be read again, no 90matter what. 91 92 <p>‘<samp><span class="samp">#pragma once</span></samp>’ does not have the problems that ‘<samp><span class="samp">#import</span></samp>’ does, 93but it is not recognized by all preprocessors, so you cannot rely on it 94in a portable program. 95 96 </body></html> 97 98