C_002b_002b-Interface.html 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2017 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Funding Free Software", the Front-Cover
  8. Texts being (a) (see below), and with the Back-Cover Texts being (b)
  9. (see below). A copy of the license is included in the section entitled
  10. "GNU Free Documentation License".
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <title>Using the GNU Compiler Collection (GCC): C++ Interface</title>
  20. <meta name="description" content="Using the GNU Compiler Collection (GCC): C++ Interface">
  21. <meta name="keywords" content="Using the GNU Compiler Collection (GCC): C++ Interface">
  22. <meta name="resource-type" content="document">
  23. <meta name="distribution" content="global">
  24. <meta name="Generator" content="makeinfo">
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="C_002b_002b-Extensions.html#C_002b_002b-Extensions" rel="up" title="C++ Extensions">
  30. <link href="Template-Instantiation.html#Template-Instantiation" rel="next" title="Template Instantiation">
  31. <link href="Vague-Linkage.html#Vague-Linkage" rel="prev" title="Vague Linkage">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.smallquotation {font-size: smaller}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.indentedblock {margin-left: 3.2em}
  39. div.lisp {margin-left: 3.2em}
  40. div.smalldisplay {margin-left: 3.2em}
  41. div.smallexample {margin-left: 3.2em}
  42. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style:oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nocodebreak {white-space:nowrap}
  54. span.nolinebreak {white-space:nowrap}
  55. span.roman {font-family:serif; font-weight:normal}
  56. span.sansserif {font-family:sans-serif; font-weight:normal}
  57. ul.no-bullet {list-style: none}
  58. -->
  59. </style>
  60. </head>
  61. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  62. <a name="C_002b_002b-Interface"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Template-Instantiation.html#Template-Instantiation" accesskey="n" rel="next">Template Instantiation</a>, Previous: <a href="Vague-Linkage.html#Vague-Linkage" accesskey="p" rel="prev">Vague Linkage</a>, Up: <a href="C_002b_002b-Extensions.html#C_002b_002b-Extensions" accesskey="u" rel="up">C++ Extensions</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  66. </div>
  67. <hr>
  68. <a name="C_002b_002b-Interface-and-Implementation-Pragmas"></a>
  69. <h3 class="section">7.4 C++ Interface and Implementation Pragmas</h3>
  70. <a name="index-interface-and-implementation-headers_002c-C_002b_002b"></a>
  71. <a name="index-C_002b_002b-interface-and-implementation-headers"></a>
  72. <a name="index-pragmas_002c-interface-and-implementation"></a>
  73. <p><code>#pragma interface</code> and <code>#pragma implementation</code> provide the
  74. user with a way of explicitly directing the compiler to emit entities
  75. with vague linkage (and debugging information) in a particular
  76. translation unit.
  77. </p>
  78. <p><em>Note:</em> These <code>#pragma</code>s have been superceded as of GCC 2.7.2
  79. by COMDAT support and the &ldquo;key method&rdquo; heuristic
  80. mentioned in <a href="Vague-Linkage.html#Vague-Linkage">Vague Linkage</a>. Using them can actually cause your
  81. program to grow due to unnecessary out-of-line copies of inline
  82. functions.
  83. </p>
  84. <dl compact="compact">
  85. <dt><code>#pragma interface</code></dt>
  86. <dt><code>#pragma interface &quot;<var>subdir</var>/<var>objects</var>.h&quot;</code></dt>
  87. <dd><a name="index-_0023pragma-interface"></a>
  88. <p>Use this directive in <em>header files</em> that define object classes, to save
  89. space in most of the object files that use those classes. Normally,
  90. local copies of certain information (backup copies of inline member
  91. functions, debugging information, and the internal tables that implement
  92. virtual functions) must be kept in each object file that includes class
  93. definitions. You can use this pragma to avoid such duplication. When a
  94. header file containing &lsquo;<samp>#pragma interface</samp>&rsquo; is included in a
  95. compilation, this auxiliary information is not generated (unless
  96. the main input source file itself uses &lsquo;<samp>#pragma implementation</samp>&rsquo;).
  97. Instead, the object files contain references to be resolved at link
  98. time.
  99. </p>
  100. <p>The second form of this directive is useful for the case where you have
  101. multiple headers with the same name in different directories. If you
  102. use this form, you must specify the same string to &lsquo;<samp>#pragma
  103. implementation</samp>&rsquo;.
  104. </p>
  105. </dd>
  106. <dt><code>#pragma implementation</code></dt>
  107. <dt><code>#pragma implementation &quot;<var>objects</var>.h&quot;</code></dt>
  108. <dd><a name="index-_0023pragma-implementation"></a>
  109. <p>Use this pragma in a <em>main input file</em>, when you want full output from
  110. included header files to be generated (and made globally visible). The
  111. included header file, in turn, should use &lsquo;<samp>#pragma interface</samp>&rsquo;.
  112. Backup copies of inline member functions, debugging information, and the
  113. internal tables used to implement virtual functions are all generated in
  114. implementation files.
  115. </p>
  116. <a name="index-implied-_0023pragma-implementation"></a>
  117. <a name="index-_0023pragma-implementation_002c-implied"></a>
  118. <a name="index-naming-convention_002c-implementation-headers"></a>
  119. <p>If you use &lsquo;<samp>#pragma implementation</samp>&rsquo; with no argument, it applies to
  120. an include file with the same basename<a name="DOCF4" href="#FOOT4"><sup>4</sup></a> as your source
  121. file. For example, in <samp>allclass.cc</samp>, giving just
  122. &lsquo;<samp>#pragma implementation</samp>&rsquo;
  123. by itself is equivalent to &lsquo;<samp>#pragma implementation &quot;allclass.h&quot;</samp>&rsquo;.
  124. </p>
  125. <p>Use the string argument if you want a single implementation file to
  126. include code from multiple header files. (You must also use
  127. &lsquo;<samp>#include</samp>&rsquo; to include the header file; &lsquo;<samp>#pragma
  128. implementation</samp>&rsquo; only specifies how to use the file&mdash;it doesn&rsquo;t actually
  129. include it.)
  130. </p>
  131. <p>There is no way to split up the contents of a single header file into
  132. multiple implementation files.
  133. </p></dd>
  134. </dl>
  135. <a name="index-inlining-and-C_002b_002b-pragmas"></a>
  136. <a name="index-C_002b_002b-pragmas_002c-effect-on-inlining"></a>
  137. <a name="index-pragmas-in-C_002b_002b_002c-effect-on-inlining"></a>
  138. <p>&lsquo;<samp>#pragma implementation</samp>&rsquo; and &lsquo;<samp>#pragma interface</samp>&rsquo; also have an
  139. effect on function inlining.
  140. </p>
  141. <p>If you define a class in a header file marked with &lsquo;<samp>#pragma
  142. interface</samp>&rsquo;, the effect on an inline function defined in that class is
  143. similar to an explicit <code>extern</code> declaration&mdash;the compiler emits
  144. no code at all to define an independent version of the function. Its
  145. definition is used only for inlining with its callers.
  146. </p>
  147. <a name="index-fno_002dimplement_002dinlines-1"></a>
  148. <p>Conversely, when you include the same header file in a main source file
  149. that declares it as &lsquo;<samp>#pragma implementation</samp>&rsquo;, the compiler emits
  150. code for the function itself; this defines a version of the function
  151. that can be found via pointers (or by callers compiled without
  152. inlining). If all calls to the function can be inlined, you can avoid
  153. emitting the function by compiling with <samp>-fno-implement-inlines</samp>.
  154. If any calls are not inlined, you will get linker errors.
  155. </p>
  156. <div class="footnote">
  157. <hr>
  158. <h4 class="footnotes-heading">Footnotes</h4>
  159. <h3><a name="FOOT4" href="#DOCF4">(4)</a></h3>
  160. <p>A file&rsquo;s <em>basename</em>
  161. is the name stripped of all leading path information and of trailing
  162. suffixes, such as &lsquo;<samp>.h</samp>&rsquo; or &lsquo;<samp>.C</samp>&rsquo; or &lsquo;<samp>.cc</samp>&rsquo;.</p>
  163. </div>
  164. <hr>
  165. <div class="header">
  166. <p>
  167. Next: <a href="Template-Instantiation.html#Template-Instantiation" accesskey="n" rel="next">Template Instantiation</a>, Previous: <a href="Vague-Linkage.html#Vague-Linkage" accesskey="p" rel="prev">Vague Linkage</a>, Up: <a href="C_002b_002b-Extensions.html#C_002b_002b-Extensions" accesskey="u" rel="up">C++ Extensions</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  168. </div>
  169. </body>
  170. </html>