1<html lang="en"> 2<head> 3<title>C++ Interface - Using the GNU Compiler Collection (GCC)</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="Using the GNU Compiler Collection (GCC)"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="C_002b_002b-Extensions.html#C_002b_002b-Extensions" title="C++ Extensions"> 9<link rel="prev" href="Vague-Linkage.html#Vague-Linkage" title="Vague Linkage"> 10<link rel="next" href="Template-Instantiation.html#Template-Instantiation" title="Template Instantiation"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<!-- 13Copyright (C) 1988-2013 Free Software Foundation, Inc. 14 15Permission is granted to copy, distribute and/or modify this document 16under the terms of the GNU Free Documentation License, Version 1.3 or 17any later version published by the Free Software Foundation; with the 18Invariant Sections being ``Funding Free Software'', the Front-Cover 19Texts being (a) (see below), and with the Back-Cover Texts being (b) 20(see below). A copy of the license is included in the section entitled 21``GNU Free Documentation License''. 22 23(a) The FSF's Front-Cover Text is: 24 25 A GNU Manual 26 27(b) The FSF's Back-Cover Text is: 28 29 You have freedom to copy and modify this GNU Manual, like GNU 30 software. Copies published by the Free Software Foundation raise 31 funds for GNU development.--> 32<meta http-equiv="Content-Style-Type" content="text/css"> 33<style type="text/css"><!-- 34 pre.display { font-family:inherit } 35 pre.format { font-family:inherit } 36 pre.smalldisplay { font-family:inherit; font-size:smaller } 37 pre.smallformat { font-family:inherit; font-size:smaller } 38 pre.smallexample { font-size:smaller } 39 pre.smalllisp { font-size:smaller } 40 span.sc { font-variant:small-caps } 41 span.roman { font-family:serif; font-weight:normal; } 42 span.sansserif { font-family:sans-serif; font-weight:normal; } 43--></style> 44<link rel="stylesheet" type="text/css" href="../cs.css"> 45</head> 46<body> 47<div class="node"> 48<a name="C++-Interface"></a> 49<a name="C_002b_002b-Interface"></a> 50<p> 51Next: <a rel="next" accesskey="n" href="Template-Instantiation.html#Template-Instantiation">Template Instantiation</a>, 52Previous: <a rel="previous" accesskey="p" href="Vague-Linkage.html#Vague-Linkage">Vague Linkage</a>, 53Up: <a rel="up" accesskey="u" href="C_002b_002b-Extensions.html#C_002b_002b-Extensions">C++ Extensions</a> 54<hr> 55</div> 56 57<h3 class="section">7.4 #pragma interface and implementation</h3> 58 59<p><a name="index-interface-and-implementation-headers_002c-C_002b_002b-3512"></a><a name="index-C_002b_002b-interface-and-implementation-headers-3513"></a><a name="index-pragmas_002c-interface-and-implementation-3514"></a> 60<code>#pragma interface</code> and <code>#pragma implementation</code> provide the 61user with a way of explicitly directing the compiler to emit entities 62with vague linkage (and debugging information) in a particular 63translation unit. 64 65 <p><em>Note:</em> As of GCC 2.7.2, these <code>#pragma</code>s are not useful in 66most cases, because of COMDAT support and the “key method” heuristic 67mentioned in <a href="Vague-Linkage.html#Vague-Linkage">Vague Linkage</a>. Using them can actually cause your 68program to grow due to unnecessary out-of-line copies of inline 69functions. Currently (3.4) the only benefit of these 70<code>#pragma</code>s is reduced duplication of debugging information, and 71that should be addressed soon on DWARF 2 targets with the use of 72COMDAT groups. 73 74 <dl> 75<dt><code>#pragma interface</code><dt><code>#pragma interface "</code><var>subdir</var><code>/</code><var>objects</var><code>.h"</code><dd><a name="index-g_t_0023pragma-interface-3515"></a>Use this directive in <em>header files</em> that define object classes, to save 76space in most of the object files that use those classes. Normally, 77local copies of certain information (backup copies of inline member 78functions, debugging information, and the internal tables that implement 79virtual functions) must be kept in each object file that includes class 80definitions. You can use this pragma to avoid such duplication. When a 81header file containing ‘<samp><span class="samp">#pragma interface</span></samp>’ is included in a 82compilation, this auxiliary information is not generated (unless 83the main input source file itself uses ‘<samp><span class="samp">#pragma implementation</span></samp>’). 84Instead, the object files contain references to be resolved at link 85time. 86 87 <p>The second form of this directive is useful for the case where you have 88multiple headers with the same name in different directories. If you 89use this form, you must specify the same string to ‘<samp><span class="samp">#pragma 90implementation</span></samp>’. 91 92 <br><dt><code>#pragma implementation</code><dt><code>#pragma implementation "</code><var>objects</var><code>.h"</code><dd><a name="index-g_t_0023pragma-implementation-3516"></a>Use this pragma in a <em>main input file</em>, when you want full output from 93included header files to be generated (and made globally visible). The 94included header file, in turn, should use ‘<samp><span class="samp">#pragma interface</span></samp>’. 95Backup copies of inline member functions, debugging information, and the 96internal tables used to implement virtual functions are all generated in 97implementation files. 98 99 <p><a name="index-implied-_0040code_007b_0023pragma-implementation_007d-3517"></a><a name="index-g_t_0040code_007b_0023pragma-implementation_007d_002c-implied-3518"></a><a name="index-naming-convention_002c-implementation-headers-3519"></a>If you use ‘<samp><span class="samp">#pragma implementation</span></samp>’ with no argument, it applies to 100an include file with the same basename<a rel="footnote" href="#fn-1" name="fnd-1"><sup>1</sup></a> as your source 101file. For example, in <samp><span class="file">allclass.cc</span></samp>, giving just 102‘<samp><span class="samp">#pragma implementation</span></samp>’ 103by itself is equivalent to ‘<samp><span class="samp">#pragma implementation "allclass.h"</span></samp>’. 104 105 <p>In versions of GNU C++ prior to 2.6.0 <samp><span class="file">allclass.h</span></samp> was treated as 106an implementation file whenever you would include it from 107<samp><span class="file">allclass.cc</span></samp> even if you never specified ‘<samp><span class="samp">#pragma 108implementation</span></samp>’. This was deemed to be more trouble than it was worth, 109however, and disabled. 110 111 <p>Use the string argument if you want a single implementation file to 112include code from multiple header files. (You must also use 113‘<samp><span class="samp">#include</span></samp>’ to include the header file; ‘<samp><span class="samp">#pragma 114implementation</span></samp>’ only specifies how to use the file—it doesn't actually 115include it.) 116 117 <p>There is no way to split up the contents of a single header file into 118multiple implementation files. 119</dl> 120 121 <p><a name="index-inlining-and-C_002b_002b-pragmas-3520"></a><a name="index-C_002b_002b-pragmas_002c-effect-on-inlining-3521"></a><a name="index-pragmas-in-C_002b_002b_002c-effect-on-inlining-3522"></a>‘<samp><span class="samp">#pragma implementation</span></samp>’ and ‘<samp><span class="samp">#pragma interface</span></samp>’ also have an 122effect on function inlining. 123 124 <p>If you define a class in a header file marked with ‘<samp><span class="samp">#pragma 125interface</span></samp>’, the effect on an inline function defined in that class is 126similar to an explicit <code>extern</code> declaration—the compiler emits 127no code at all to define an independent version of the function. Its 128definition is used only for inlining with its callers. 129 130 <p><a name="index-fno_002dimplement_002dinlines-3523"></a>Conversely, when you include the same header file in a main source file 131that declares it as ‘<samp><span class="samp">#pragma implementation</span></samp>’, the compiler emits 132code for the function itself; this defines a version of the function 133that can be found via pointers (or by callers compiled without 134inlining). If all calls to the function can be inlined, you can avoid 135emitting the function by compiling with <samp><span class="option">-fno-implement-inlines</span></samp>. 136If any calls are not inlined, you will get linker errors. 137 138 <div class="footnote"> 139<hr> 140<h4>Footnotes</h4><p class="footnote"><small>[<a name="fn-1" href="#fnd-1">1</a>]</small> A file's <dfn>basename</dfn> 141is the name stripped of all leading path information and of trailing 142suffixes, such as ‘<samp><span class="samp">.h</span></samp>’ or ‘<samp><span class="samp">.C</span></samp>’ or ‘<samp><span class="samp">.cc</span></samp>’.</p> 143 144 <hr></div> 145 146 </body></html> 147 148