Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

219 lines
10KB

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