Precompiled-Headers.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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): Precompiled Headers</title>
  20. <meta name="description" content="Using the GNU Compiler Collection (GCC): Precompiled Headers">
  21. <meta name="keywords" content="Using the GNU Compiler Collection (GCC): Precompiled Headers">
  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="Invoking-GCC.html#Invoking-GCC" rel="up" title="Invoking GCC">
  30. <link href="C-Implementation.html#C-Implementation" rel="next" title="C Implementation">
  31. <link href="Environment-Variables.html#Environment-Variables" rel="prev" title="Environment Variables">
  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="Precompiled-Headers"></a>
  63. <div class="header">
  64. <p>
  65. Previous: <a href="Environment-Variables.html#Environment-Variables" accesskey="p" rel="prev">Environment Variables</a>, Up: <a href="Invoking-GCC.html#Invoking-GCC" accesskey="u" rel="up">Invoking GCC</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="Using-Precompiled-Headers"></a>
  69. <h3 class="section">3.21 Using Precompiled Headers</h3>
  70. <a name="index-precompiled-headers"></a>
  71. <a name="index-speed-of-compilation"></a>
  72. <p>Often large projects have many header files that are included in every
  73. source file. The time the compiler takes to process these header files
  74. over and over again can account for nearly all of the time required to
  75. build the project. To make builds faster, GCC allows you to
  76. <em>precompile</em> a header file.
  77. </p>
  78. <p>To create a precompiled header file, simply compile it as you would any
  79. other file, if necessary using the <samp>-x</samp> option to make the driver
  80. treat it as a C or C++ header file. You may want to use a
  81. tool like <code>make</code> to keep the precompiled header up-to-date when
  82. the headers it contains change.
  83. </p>
  84. <p>A precompiled header file is searched for when <code>#include</code> is
  85. seen in the compilation. As it searches for the included file
  86. (see <a href="http://gcc.gnu.org/onlinedocs/cpp/Search-Path.html#Search-Path">Search Path</a> in <cite>The C Preprocessor</cite>) the
  87. compiler looks for a precompiled header in each directory just before it
  88. looks for the include file in that directory. The name searched for is
  89. the name specified in the <code>#include</code> with &lsquo;<samp>.gch</samp>&rsquo; appended. If
  90. the precompiled header file cannot be used, it is ignored.
  91. </p>
  92. <p>For instance, if you have <code>#include &quot;all.h&quot;</code>, and you have
  93. <samp>all.h.gch</samp> in the same directory as <samp>all.h</samp>, then the
  94. precompiled header file is used if possible, and the original
  95. header is used otherwise.
  96. </p>
  97. <p>Alternatively, you might decide to put the precompiled header file in a
  98. directory and use <samp>-I</samp> to ensure that directory is searched
  99. before (or instead of) the directory containing the original header.
  100. Then, if you want to check that the precompiled header file is always
  101. used, you can put a file of the same name as the original header in this
  102. directory containing an <code>#error</code> command.
  103. </p>
  104. <p>This also works with <samp>-include</samp>. So yet another way to use
  105. precompiled headers, good for projects not designed with precompiled
  106. header files in mind, is to simply take most of the header files used by
  107. a project, include them from another header file, precompile that header
  108. file, and <samp>-include</samp> the precompiled header. If the header files
  109. have guards against multiple inclusion, they are skipped because
  110. they&rsquo;ve already been included (in the precompiled header).
  111. </p>
  112. <p>If you need to precompile the same header file for different
  113. languages, targets, or compiler options, you can instead make a
  114. <em>directory</em> named like <samp>all.h.gch</samp>, and put each precompiled
  115. header in the directory, perhaps using <samp>-o</samp>. It doesn&rsquo;t matter
  116. what you call the files in the directory; every precompiled header in
  117. the directory is considered. The first precompiled header
  118. encountered in the directory that is valid for this compilation is
  119. used; they&rsquo;re searched in no particular order.
  120. </p>
  121. <p>There are many other possibilities, limited only by your imagination,
  122. good sense, and the constraints of your build system.
  123. </p>
  124. <p>A precompiled header file can be used only when these conditions apply:
  125. </p>
  126. <ul>
  127. <li> Only one precompiled header can be used in a particular compilation.
  128. </li><li> A precompiled header cannot be used once the first C token is seen. You
  129. can have preprocessor directives before a precompiled header; you cannot
  130. include a precompiled header from inside another header.
  131. </li><li> The precompiled header file must be produced for the same language as
  132. the current compilation. You cannot use a C precompiled header for a C++
  133. compilation.
  134. </li><li> The precompiled header file must have been produced by the same compiler
  135. binary as the current compilation is using.
  136. </li><li> Any macros defined before the precompiled header is included must
  137. either be defined in the same way as when the precompiled header was
  138. generated, or must not affect the precompiled header, which usually
  139. means that they don&rsquo;t appear in the precompiled header at all.
  140. <p>The <samp>-D</samp> option is one way to define a macro before a
  141. precompiled header is included; using a <code>#define</code> can also do it.
  142. There are also some options that define macros implicitly, like
  143. <samp>-O</samp> and <samp>-Wdeprecated</samp>; the same rule applies to macros
  144. defined this way.
  145. </p>
  146. </li><li> If debugging information is output when using the precompiled
  147. header, using <samp>-g</samp> or similar, the same kind of debugging information
  148. must have been output when building the precompiled header. However,
  149. a precompiled header built using <samp>-g</samp> can be used in a compilation
  150. when no debugging information is being output.
  151. </li><li> The same <samp>-m</samp> options must generally be used when building
  152. and using the precompiled header. See <a href="Submodel-Options.html#Submodel-Options">Submodel Options</a>,
  153. for any cases where this rule is relaxed.
  154. </li><li> Each of the following options must be the same when building and using
  155. the precompiled header:
  156. <div class="smallexample">
  157. <pre class="smallexample">-fexceptions
  158. </pre></div>
  159. </li><li> Some other command-line options starting with <samp>-f</samp>,
  160. <samp>-p</samp>, or <samp>-O</samp> must be defined in the same way as when
  161. the precompiled header was generated. At present, it&rsquo;s not clear
  162. which options are safe to change and which are not; the safest choice
  163. is to use exactly the same options when generating and using the
  164. precompiled header. The following are known to be safe:
  165. <div class="smallexample">
  166. <pre class="smallexample">-fmessage-length= -fpreprocessed -fsched-interblock
  167. -fsched-spec -fsched-spec-load -fsched-spec-load-dangerous
  168. -fsched-verbose=<var>number</var> -fschedule-insns -fvisibility=
  169. -pedantic-errors
  170. </pre></div>
  171. </li></ul>
  172. <p>For all of these except the last, the compiler automatically
  173. ignores the precompiled header if the conditions aren&rsquo;t met. If you
  174. find an option combination that doesn&rsquo;t work and doesn&rsquo;t cause the
  175. precompiled header to be ignored, please consider filing a bug report,
  176. see <a href="Bugs.html#Bugs">Bugs</a>.
  177. </p>
  178. <p>If you do use differing options when generating and using the
  179. precompiled header, the actual behavior is a mixture of the
  180. behavior for the options. For instance, if you use <samp>-g</samp> to
  181. generate the precompiled header but not when using it, you may or may
  182. not get debugging information for routines in the precompiled header.
  183. </p>
  184. <hr>
  185. <div class="header">
  186. <p>
  187. Previous: <a href="Environment-Variables.html#Environment-Variables" accesskey="p" rel="prev">Environment Variables</a>, Up: <a href="Invoking-GCC.html#Invoking-GCC" accesskey="u" rel="up">Invoking GCC</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>
  188. </div>
  189. </body>
  190. </html>