📄 c---interface.html
字号:
<html lang="en"><head><title>Using the GNU Compiler Collection (GCC)</title><meta http-equiv="Content-Type" content="text/html"><meta name="description" content="Using the GNU Compiler Collection (GCC)"><meta name="generator" content="makeinfo 4.6"><!--Copyright © 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc. <p>Permission is granted to copy, distribute and/or modify this documentunder the terms of the GNU Free Documentation License, Version 1.2 orany later version published by the Free Software Foundation; with theInvariant Sections being "GNU General Public License" and "FundingFree Software", the Front-Cover texts being (a) (see below), and withthe Back-Cover Texts being (b) (see below). A copy of the license isincluded in the section entitled "GNU Free Documentation License". <p>(a) The FSF's Front-Cover Text is: <p>A GNU Manual <p>(b) The FSF's Back-Cover Text is: <p>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 }--></style></head><body><div class="node"><p>Node: <a name="C++%20Interface">C++ Interface</a>,Next: <a rel="next" accesskey="n" href="Template-Instantiation.html#Template%20Instantiation">Template Instantiation</a>,Previous: <a rel="previous" accesskey="p" href="Vague-Linkage.html#Vague%20Linkage">Vague Linkage</a>,Up: <a rel="up" accesskey="u" href="C---Extensions.html#C++%20Extensions">C++ Extensions</a><hr><br></div><h3 class="section">Declarations and Definitions in One Header</h3><p>C++ object definitions can be quite complex. In principle, your sourcecode will need two kinds of things for each object that you use acrossmore than one source file. First, you need an <dfn>interface</dfn>specification, describing its structure with type declarations andfunction prototypes. Second, you need the <dfn>implementation</dfn> itself. It can be tedious to maintain a separate interface description in aheader file, in parallel to the actual implementation. It is alsodangerous, since separate interface and implementation definitions maynot remain parallel. <p>With GNU C++, you can use a single header file for both purposes. <blockquote><em>Warning:</em> The mechanism to specify this is in transition. For thenonce, you must use one of two <code>#pragma</code> commands; in a futurerelease of GNU C++, an alternative mechanism will make these<code>#pragma</code> commands unnecessary. </blockquote> <p>The header file contains the full definitions, but is marked with<code>#pragma interface</code> in the source code. This allows the compilerto use the header file only as an interface specification when ordinarysource files incorporate it with <code>#include</code>. In the single sourcefile where the full implementation belongs, you can use either a namingconvention or <code>#pragma implementation</code> to indicate this alternateuse of the header file. <dl><dt><code>#pragma interface</code> <dd><dt><code>#pragma interface "</code><var>subdir</var><code>/</code><var>objects</var><code>.h"</code> <dd>Use this directive in <em>header files</em> that define object classes, to savespace in most of the object files that use those classes. Normally,local copies of certain information (backup copies of inline memberfunctions, debugging information, and the internal tables that implementvirtual functions) must be kept in each object file that includes classdefinitions. You can use this pragma to avoid such duplication. When aheader file containing <code>#pragma interface</code> is included in acompilation, this auxiliary information will not be generated (unlessthe main input source file itself uses <code>#pragma implementation</code>). Instead, the object files will contain references to be resolved at linktime. <p>The second form of this directive is useful for the case where you havemultiple headers with the same name in different directories. If youuse this form, you must specify the same string to <code>#pragmaimplementation</code>. <br><dt><code>#pragma implementation</code> <dd><dt><code>#pragma implementation "</code><var>objects</var><code>.h"</code> <dd>Use this pragma in a <em>main input file</em>, when you want full output fromincluded header files to be generated (and made globally visible). Theincluded header file, in turn, should use <code>#pragma interface</code>. Backup copies of inline member functions, debugging information, and theinternal tables used to implement virtual functions are all generated inimplementation files. <p>If you use <code>#pragma implementation</code> with no argument, it applies toan include file with the same basename<a rel="footnote" href="#fn-1"><sup>1</sup></a> as your sourcefile. For example, in <code>allclass.cc</code>, giving just<code>#pragma implementation</code>by itself is equivalent to <code>#pragma implementation "allclass.h"</code>. <p>In versions of GNU C++ prior to 2.6.0 <code>allclass.h</code> was treated asan implementation file whenever you would include it from<code>allclass.cc</code> even if you never specified <code>#pragmaimplementation</code>. This was deemed to be more trouble than it was worth,however, and disabled. <p>If you use an explicit <code>#pragma implementation</code>, it must appear inyour source file <em>before</em> you include the affected header files. <p>Use the string argument if you want a single implementation file toinclude code from multiple header files. (You must also use<code>#include</code> to include the header file; <code>#pragmaimplementation</code> only specifies how to use the file--it doesn't actuallyinclude it.) <p>There is no way to split up the contents of a single header file intomultiple implementation files. </dl> <p><code>#pragma implementation</code> and <code>#pragma interface</code> also have aneffect on function inlining. <p>If you define a class in a header file marked with <code>#pragmainterface</code>, the effect on a function defined in that class is similar toan explicit <code>extern</code> declaration--the compiler emits no code atall to define an independent version of the function. Its definitionis used only for inlining with its callers. <p>Conversely, when you include the same header file in a main source filethat declares it as <code>#pragma implementation</code>, the compiler emitscode for the function itself; this defines a version of the functionthat can be found via pointers (or by callers compiled withoutinlining). If all calls to the function can be inlined, you can avoidemitting the function by compiling with <code>-fno-implement-inlines</code>. If any calls were not inlined, you will get linker errors. <div class="footnote"><hr><h4>Footnotes</h4><ol type="1"><li><a name="fn-1"></a><p>A file's <dfn>basename</dfn>was the name stripped of all leading path information and of trailingsuffixes, such as <code>.h</code> or <code>.C</code> or <code>.cc</code>.</p> </ol><hr></div> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -