📄 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.3">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home">
<!--
Copyright © 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
<p>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; with the
Invariant Sections being "GNU General Public License" and "Funding
Free Software", the Front-Cover texts being (a) (see below), and with
the Back-Cover Texts being (b) (see below). A copy of the license is
included 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.-->
</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 source
code will need two kinds of things for each object that you use across
more than one source file. First, you need an <dfn>interface</dfn>
specification, describing its structure with type declarations and
function prototypes. Second, you need the <dfn>implementation</dfn> itself.
It can be tedious to maintain a separate interface description in a
header file, in parallel to the actual implementation. It is also
dangerous, since separate interface and implementation definitions may
not 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 the
nonce, you must use one of two <code>#pragma</code> commands; in a future
release 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 compiler
to use the header file only as an interface specification when ordinary
source files incorporate it with <code>#include</code>. In the single source
file where the full implementation belongs, you can use either a naming
convention or <code>#pragma implementation</code> to indicate this alternate
use 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 save
space in most of the object files that use those classes. Normally,
local copies of certain information (backup copies of inline member
functions, debugging information, and the internal tables that implement
virtual functions) must be kept in each object file that includes class
definitions. You can use this pragma to avoid such duplication. When a
header file containing <code>#pragma interface</code> is included in a
compilation, this auxiliary information will not be generated (unless
the main input source file itself uses <code>#pragma implementation</code>).
Instead, the object files will contain references to be resolved at link
time.
<p>The second form of this directive is useful for the case where you have
multiple headers with the same name in different directories. If you
use this form, you must specify the same string to <code>#pragma
implementation</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 from
included header files to be generated (and made globally visible). The
included header file, in turn, should use <code>#pragma interface</code>.
Backup copies of inline member functions, debugging information, and the
internal tables used to implement virtual functions are all generated in
implementation files.
<p>If you use <code>#pragma implementation</code> with no argument, it applies to
an include file with the same basename<a rel="footnote" href="#fn-1"><sup>1</sup></a> as your source
file. 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 as
an implementation file whenever you would include it from
<code>allclass.cc</code> even if you never specified <code>#pragma
implementation</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 in
your source file <em>before</em> you include the affected header files.
<p>Use the string argument if you want a single implementation file to
include code from multiple header files. (You must also use
<code>#include</code> to include the header file; <code>#pragma
implementation</code> only specifies how to use the file--it doesn't actually
include it.)
<p>There is no way to split up the contents of a single header file into
multiple implementation files.
</dl>
<p><code>#pragma implementation</code> and <code>#pragma interface</code> also have an
effect on function inlining.
<p>If you define a class in a header file marked with <code>#pragma
interface</code>, the effect on a function defined in that class is similar to
an explicit <code>extern</code> declaration--the compiler emits no code at
all to define an independent version of the function. Its definition
is used only for inlining with its callers.
<p>Conversely, when you include the same header file in a main source file
that declares it as <code>#pragma implementation</code>, the compiler emits
code for the function itself; this defines a version of the function
that can be found via pointers (or by callers compiled without
inlining). If all calls to the function can be inlined, you can avoid
emitting 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 trailing
suffixes, 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 + -