| <html lang="en"> |
| <head> |
| <title>Alternatives to Wrapper #ifndef - The C Preprocessor</title> |
| <meta http-equiv="Content-Type" content="text/html"> |
| <meta name="description" content="The C Preprocessor"> |
| <meta name="generator" content="makeinfo 4.13"> |
| <link title="Top" rel="start" href="index.html#Top"> |
| <link rel="up" href="Header-Files.html#Header-Files" title="Header Files"> |
| <link rel="prev" href="Once_002dOnly-Headers.html#Once_002dOnly-Headers" title="Once-Only Headers"> |
| <link rel="next" href="Computed-Includes.html#Computed-Includes" title="Computed Includes"> |
| <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> |
| <!-- |
| Copyright (C) 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996, |
| 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, |
| 2008, 2009, 2010 |
| Free Software Foundation, Inc. |
| |
| Permission is granted to copy, distribute and/or modify this document |
| under the terms of the GNU Free Documentation License, Version 1.2 or |
| any later version published by the Free Software Foundation. A copy of |
| the license is included in the |
| section entitled ``GNU Free Documentation License''. |
| |
| This manual contains no Invariant Sections. The Front-Cover Texts are |
| (a) (see below), and the Back-Cover Texts are (b) (see below). |
| |
| (a) The FSF's Front-Cover Text is: |
| |
| A GNU Manual |
| |
| (b) The FSF's Back-Cover Text is: |
| |
| You have freedom to copy and modify this GNU Manual, like GNU |
| software. Copies published by the Free Software Foundation raise |
| funds for GNU development. |
| --> |
| <meta http-equiv="Content-Style-Type" content="text/css"> |
| <style type="text/css"><!-- |
| pre.display { font-family:inherit } |
| pre.format { font-family:inherit } |
| pre.smalldisplay { font-family:inherit; font-size:smaller } |
| pre.smallformat { font-family:inherit; font-size:smaller } |
| pre.smallexample { font-size:smaller } |
| pre.smalllisp { font-size:smaller } |
| span.sc { font-variant:small-caps } |
| span.roman { font-family:serif; font-weight:normal; } |
| span.sansserif { font-family:sans-serif; font-weight:normal; } |
| --></style> |
| <link rel="stylesheet" type="text/css" href="../cs.css"> |
| </head> |
| <body> |
| <div class="node"> |
| <a name="Alternatives-to-Wrapper-%23ifndef"></a> |
| <a name="Alternatives-to-Wrapper-_0023ifndef"></a> |
| <p> |
| Next: <a rel="next" accesskey="n" href="Computed-Includes.html#Computed-Includes">Computed Includes</a>, |
| Previous: <a rel="previous" accesskey="p" href="Once_002dOnly-Headers.html#Once_002dOnly-Headers">Once-Only Headers</a>, |
| Up: <a rel="up" accesskey="u" href="Header-Files.html#Header-Files">Header Files</a> |
| <hr> |
| </div> |
| |
| <h3 class="section">2.5 Alternatives to Wrapper #ifndef</h3> |
| |
| <p>CPP supports two more ways of indicating that a header file should be |
| read only once. Neither one is as portable as a wrapper ‘<samp><span class="samp">#ifndef</span></samp>’ |
| and we recommend you do not use them in new programs, with the caveat |
| that ‘<samp><span class="samp">#import</span></samp>’ is standard practice in Objective-C. |
| |
| <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 |
| includes a file, but does so at most once. If you use ‘<samp><span class="samp">#import</span></samp>’ |
| instead of ‘<samp><span class="samp">#include</span></samp>’, then you don't need the conditionals |
| inside the header file to prevent multiple inclusion of the contents. |
| ‘<samp><span class="samp">#import</span></samp>’ is standard in Objective-C, but is considered a |
| deprecated extension in C and C++. |
| |
| <p>‘<samp><span class="samp">#import</span></samp>’ is not a well designed feature. It requires the users of |
| a header file to know that it should only be included once. It is much |
| better for the header file's implementor to write the file so that users |
| don't need to know this. Using a wrapper ‘<samp><span class="samp">#ifndef</span></samp>’ accomplishes |
| this goal. |
| |
| <p>In the present implementation, a single use of ‘<samp><span class="samp">#import</span></samp>’ will |
| prevent the file from ever being read again, by either ‘<samp><span class="samp">#import</span></samp>’ or |
| ‘<samp><span class="samp">#include</span></samp>’. You should not rely on this; do not use both |
| ‘<samp><span class="samp">#import</span></samp>’ and ‘<samp><span class="samp">#include</span></samp>’ to refer to the same header file. |
| |
| <p>Another way to prevent a header file from being included more than once |
| is with the ‘<samp><span class="samp">#pragma once</span></samp>’ directive. If ‘<samp><span class="samp">#pragma once</span></samp>’ is |
| seen when scanning a header file, that file will never be read again, no |
| matter what. |
| |
| <p>‘<samp><span class="samp">#pragma once</span></samp>’ does not have the problems that ‘<samp><span class="samp">#import</span></samp>’ does, |
| but it is not recognized by all preprocessors, so you cannot rely on it |
| in a portable program. |
| |
| </body></html> |
| |