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