1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<!-- Created by GNU Texinfo 6.8, https://www.gnu.org/software/texinfo/ -->
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
6<!-- Copyright (C) 1988-2022 Free Software Foundation, Inc.
7
8Permission is granted to copy, distribute and/or modify this document
9under the terms of the GNU Free Documentation License, Version 1.3 or
10any later version published by the Free Software Foundation; with no
11Invariant Sections, the Front-Cover texts being (a) (see below), and
12with the Back-Cover Texts being (b) (see below).  A copy of the
13license is included in the section entitled "GNU
14Free Documentation License".
15
16(a) The FSF's Front-Cover Text is:
17
18A GNU Manual
19
20(b) The FSF's Back-Cover Text is:
21
22You have freedom to copy and modify this GNU Manual, like GNU
23     software.  Copies published by the Free Software Foundation raise
24     funds for GNU development. -->
25<title>Installing GCC: Building</title>
26
27<meta name="description" content="Installing GCC: Building">
28<meta name="keywords" content="Installing GCC: Building">
29<meta name="resource-type" content="document">
30<meta name="distribution" content="global">
31<meta name="Generator" content="makeinfo">
32<meta name="viewport" content="width=device-width,initial-scale=1">
33
34<style type="text/css">
35<!--
36a.copiable-anchor {visibility: hidden; text-decoration: none; line-height: 0em}
37a.summary-letter {text-decoration: none}
38blockquote.indentedblock {margin-right: 0em}
39div.display {margin-left: 3.2em}
40div.example {margin-left: 3.2em}
41kbd {font-style: oblique}
42pre.display {font-family: inherit}
43pre.format {font-family: inherit}
44pre.menu-comment {font-family: serif}
45pre.menu-preformatted {font-family: serif}
46span.nolinebreak {white-space: nowrap}
47span.roman {font-family: initial; font-weight: normal}
48span.sansserif {font-family: sans-serif; font-weight: normal}
49span:hover a.copiable-anchor {visibility: visible}
50ul.no-bullet {list-style: none}
51-->
52</style>
53
54
55</head>
56
57<body lang="en">
58<h1 class="settitle" align="center">Installing GCC: Building</h1>
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79<span id="index-Installing-GCC_003a-Building"></span>
80
81<p>Now that GCC is configured, you are ready to build the compiler and
82runtime libraries.
83</p>
84<p>Some commands executed when making the compiler may fail (return a
85nonzero status) and be ignored by <code>make</code>.  These failures, which
86are often due to files that were not found, are expected, and can safely
87be ignored.
88</p>
89<p>It is normal to have compiler warnings when compiling certain files.
90Unless you are a GCC developer, you can generally ignore these warnings
91unless they cause compilation to fail.  Developers should attempt to fix
92any warnings encountered, however they can temporarily continue past
93warnings-as-errors by specifying the configure flag
94<samp>--disable-werror</samp>.
95</p>
96<p>On certain old systems, defining certain environment variables such as
97<code>CC</code> can interfere with the functioning of <code>make</code>.
98</p>
99<p>If you encounter seemingly strange errors when trying to build the
100compiler in a directory other than the source directory, it could be
101because you have previously configured the compiler in the source
102directory.  Make sure you have done all the necessary preparations.
103</p>
104<p>If you build GCC on a BSD system using a directory stored in an old System
105V file system, problems may occur in running <code>fixincludes</code> if the
106System V file system doesn&rsquo;t support symbolic links.  These problems
107result in a failure to fix the declaration of <code>size_t</code> in
108<samp>sys/types.h</samp>.  If you find that <code>size_t</code> is a signed type and
109that type mismatches occur, this could be the cause.
110</p>
111<p>The solution is not to use such a directory for building GCC.
112</p>
113<p>Similarly, when building from the source repository or snapshots, or if you modify
114<samp>*.l</samp> files, you need the Flex lexical analyzer generator
115installed.  If you do not modify <samp>*.l</samp> files, releases contain
116the Flex-generated files and you do not need Flex installed to build
117them.  There is still one Flex-based lexical analyzer (part of the
118build machinery, not of GCC itself) that is used even if you only
119build the C front end.
120</p>
121<p>When building from the source repository or snapshots, or if you modify Texinfo
122documentation, you need version 4.7 or later of Texinfo installed if you
123want Info documentation to be regenerated.  Releases contain Info
124documentation pre-built for the unmodified documentation in the release.
125</p>
126<div class="section" id="Building-a-native-compiler">
127<h3 class="section">Building a native compiler</h3>
128
129<p>For a native build, the default configuration is to perform
130a 3-stage bootstrap of the compiler when &lsquo;<samp>make</samp>&rsquo; is invoked.
131This will build the entire GCC system and ensure that it compiles
132itself correctly.  It can be disabled with the <samp>--disable-bootstrap</samp>
133parameter to &lsquo;<samp>configure</samp>&rsquo;, but bootstrapping is suggested because
134the compiler will be tested more completely and could also have
135better performance.
136</p>
137<p>The bootstrapping process will complete the following steps:
138</p>
139<ul>
140<li> Build tools necessary to build the compiler.
141
142</li><li> Perform a 3-stage bootstrap of the compiler.  This includes building
143three times the target tools for use by the compiler such as binutils
144(bfd, binutils, gas, gprof, ld, and opcodes) if they have been
145individually linked or moved into the top level GCC source tree before
146configuring.
147
148</li><li> Perform a comparison test of the stage2 and stage3 compilers.
149
150</li><li> Build runtime libraries using the stage3 compiler from the previous step.
151
152</li></ul>
153
154<p>If you are short on disk space you might consider &lsquo;<samp>make
155bootstrap-lean</samp>&rsquo; instead.  The sequence of compilation is the
156same described above, but object files from the stage1 and
157stage2 of the 3-stage bootstrap of the compiler are deleted as
158soon as they are no longer needed.
159</p>
160<p>If you wish to use non-default GCC flags when compiling the stage2
161and stage3 compilers, set <code>BOOT_CFLAGS</code> on the command line when
162doing &lsquo;<samp>make</samp>&rsquo;.  For example, if you want to save additional space
163during the bootstrap and in the final installation as well, you can
164build the compiler binaries without debugging information as in the
165following example.  This will save roughly 40% of disk space both for
166the bootstrap and the final installation.  (Libraries will still contain
167debugging information.)
168</p>
169<div class="example">
170<pre class="example">make BOOT_CFLAGS='-O' bootstrap
171</pre></div>
172
173<p>You can place non-default optimization flags into <code>BOOT_CFLAGS</code>; they
174are less well tested here than the default of &lsquo;<samp>-g -O2</samp>&rsquo;, but should
175still work.  In a few cases, you may find that you need to specify special
176flags such as <samp>-msoft-float</samp> here to complete the bootstrap; or,
177if the native compiler miscompiles the stage1 compiler, you may need
178to work around this, by choosing <code>BOOT_CFLAGS</code> to avoid the parts
179of the stage1 compiler that were miscompiled, or by using &lsquo;<samp>make
180bootstrap4</samp>&rsquo; to increase the number of stages of bootstrap.
181</p>
182<p><code>BOOT_CFLAGS</code> does not apply to bootstrapped target libraries.
183Since these are always compiled with the compiler currently being
184bootstrapped, you can use <code>CFLAGS_FOR_TARGET</code> to modify their
185compilation flags, as for non-bootstrapped target libraries.
186Again, if the native compiler miscompiles the stage1 compiler, you may
187need to work around this by avoiding non-working parts of the stage1
188compiler.  Use <code>STAGE1_TFLAGS</code> to this end.
189</p>
190<p>If you used the flag <samp>--enable-languages=&hellip;</samp> to restrict
191the compilers to be built, only those you&rsquo;ve actually enabled will be
192built.  This will of course only build those runtime libraries, for
193which the particular compiler has been built.  Please note,
194that re-defining <code>LANGUAGES</code> when calling &lsquo;<samp>make</samp>&rsquo;
195<strong>does not</strong> work anymore!
196</p>
197<p>If the comparison of stage2 and stage3 fails, this normally indicates
198that the stage2 compiler has compiled GCC incorrectly, and is therefore
199a potentially serious bug which you should investigate and report.  (On
200a few systems, meaningful comparison of object files is impossible; they
201always appear &ldquo;different&rdquo;.  If you encounter this problem, you will
202need to disable comparison in the <samp>Makefile</samp>.)
203</p>
204<p>If you do not want to bootstrap your compiler, you can configure with
205<samp>--disable-bootstrap</samp>.  In particular cases, you may want to
206bootstrap your compiler even if the target system is not the same as
207the one you are building on: for example, you could build a
208<code>powerpc-unknown-linux-gnu</code> toolchain on a
209<code>powerpc64-unknown-linux-gnu</code> host.  In this case, pass
210<samp>--enable-bootstrap</samp> to the configure script.
211</p>
212<p><code>BUILD_CONFIG</code> can be used to bring in additional customization
213to the build.  It can be set to a whitespace-separated list of names.
214For each such <code>NAME</code>, top-level <samp>config/<code>NAME</code>.mk</samp> will
215be included by the top-level <samp>Makefile</samp>, bringing in any settings
216it contains.  The default <code>BUILD_CONFIG</code> can be set using the
217configure option <samp>--with-build-config=<code>NAME</code>...</samp>.  Some
218examples of supported build configurations are:
219</p>
220<dl compact="compact">
221<dt><span>&lsquo;<samp>bootstrap-O1</samp>&rsquo;</span></dt>
222<dd><p>Removes any <samp>-O</samp>-started option from <code>BOOT_CFLAGS</code>, and adds
223<samp>-O1</samp> to it.  &lsquo;<samp>BUILD_CONFIG=bootstrap-O1</samp>&rsquo; is equivalent to
224&lsquo;<samp>BOOT_CFLAGS='-g -O1'</samp>&rsquo;.
225</p>
226</dd>
227<dt><span>&lsquo;<samp>bootstrap-O3</samp>&rsquo;</span></dt>
228<dt><span>&lsquo;<samp>bootstrap-Og</samp>&rsquo;</span></dt>
229<dd><p>Analogous to <code>bootstrap-O1</code>.
230</p>
231</dd>
232<dt><span>&lsquo;<samp>bootstrap-lto</samp>&rsquo;</span></dt>
233<dd><p>Enables Link-Time Optimization for host tools during bootstrapping.
234&lsquo;<samp>BUILD_CONFIG=bootstrap-lto</samp>&rsquo; is equivalent to adding
235<samp>-flto</samp> to &lsquo;<samp>BOOT_CFLAGS</samp>&rsquo;.  This option assumes that the host
236supports the linker plugin (e.g. GNU ld version 2.21 or later or GNU gold
237version 2.21 or later).
238</p>
239</dd>
240<dt><span>&lsquo;<samp>bootstrap-lto-noplugin</samp>&rsquo;</span></dt>
241<dd><p>This option is similar to <code>bootstrap-lto</code>, but is intended for
242hosts that do not support the linker plugin.  Without the linker plugin 
243static libraries are not compiled with link-time optimizations.  Since 
244the GCC middle end and back end are in <samp>libbackend.a</samp> this means
245that only the front end is actually LTO optimized.
246</p>
247</dd>
248<dt><span>&lsquo;<samp>bootstrap-lto-lean</samp>&rsquo;</span></dt>
249<dd><p>This option is similar to <code>bootstrap-lto</code>, but is intended for
250faster build by only using LTO in the final bootstrap stage.
251With &lsquo;<samp>make profiledbootstrap</samp>&rsquo; the LTO frontend
252is trained only on generator files.
253</p>
254</dd>
255<dt><span>&lsquo;<samp>bootstrap-debug</samp>&rsquo;</span></dt>
256<dd><p>Verifies that the compiler generates the same executable code, whether
257or not it is asked to emit debug information.  To this end, this
258option builds stage2 host programs without debug information, and uses
259<samp>contrib/compare-debug</samp> to compare them with the stripped stage3
260object files.  If <code>BOOT_CFLAGS</code> is overridden so as to not enable
261debug information, stage2 will have it, and stage3 won&rsquo;t.  This option
262is enabled by default when GCC bootstrapping is enabled, if
263<code>strip</code> can turn object files compiled with and without debug
264info into identical object files.  In addition to better test
265coverage, this option makes default bootstraps faster and leaner.
266</p>
267</dd>
268<dt><span>&lsquo;<samp>bootstrap-debug-big</samp>&rsquo;</span></dt>
269<dd><p>Rather than comparing stripped object files, as in
270<code>bootstrap-debug</code>, this option saves internal compiler dumps
271during stage2 and stage3 and compares them as well, which helps catch
272additional potential problems, but at a great cost in terms of disk
273space.  It can be specified in addition to &lsquo;<samp>bootstrap-debug</samp>&rsquo;.
274</p>
275</dd>
276<dt><span>&lsquo;<samp>bootstrap-debug-lean</samp>&rsquo;</span></dt>
277<dd><p>This option saves disk space compared with <code>bootstrap-debug-big</code>,
278but at the expense of some recompilation.  Instead of saving the dumps
279of stage2 and stage3 until the final compare, it uses
280<samp>-fcompare-debug</samp> to generate, compare and remove the dumps
281during stage3, repeating the compilation that already took place in
282stage2, whose dumps were not saved.
283</p>
284</dd>
285<dt><span>&lsquo;<samp>bootstrap-debug-lib</samp>&rsquo;</span></dt>
286<dd><p>This option tests executable code invariance over debug information
287generation on target libraries, just like <code>bootstrap-debug-lean</code>
288tests it on host programs.  It builds stage3 libraries with
289<samp>-fcompare-debug</samp>, and it can be used along with any of the
290<code>bootstrap-debug</code> options above.
291</p>
292<p>There aren&rsquo;t <code>-lean</code> or <code>-big</code> counterparts to this option
293because most libraries are only build in stage3, so bootstrap compares
294would not get significant coverage.  Moreover, the few libraries built
295in stage2 are used in stage3 host programs, so we wouldn&rsquo;t want to
296compile stage2 libraries with different options for comparison purposes.
297</p>
298</dd>
299<dt><span>&lsquo;<samp>bootstrap-debug-ckovw</samp>&rsquo;</span></dt>
300<dd><p>Arranges for error messages to be issued if the compiler built on any
301stage is run without the option <samp>-fcompare-debug</samp>.  This is
302useful to verify the full <samp>-fcompare-debug</samp> testing coverage.  It
303must be used along with <code>bootstrap-debug-lean</code> and
304<code>bootstrap-debug-lib</code>.
305</p>
306</dd>
307<dt><span>&lsquo;<samp>bootstrap-cet</samp>&rsquo;</span></dt>
308<dd><p>This option enables Intel CET for host tools during bootstrapping.
309&lsquo;<samp>BUILD_CONFIG=bootstrap-cet</samp>&rsquo; is equivalent to adding
310<samp>-fcf-protection</samp> to &lsquo;<samp>BOOT_CFLAGS</samp>&rsquo;.  This option
311assumes that the host supports Intel CET (e.g. GNU assembler version
3122.30 or later).
313</p>
314</dd>
315<dt><span>&lsquo;<samp>bootstrap-time</samp>&rsquo;</span></dt>
316<dd><p>Arranges for the run time of each program started by the GCC driver,
317built in any stage, to be logged to <samp>time.log</samp>, in the top level of
318the build tree.
319</p>
320</dd>
321<dt><span>&lsquo;<samp>bootstrap-asan</samp>&rsquo;</span></dt>
322<dd><p>Compiles GCC itself using Address Sanitization in order to catch invalid memory
323accesses within the GCC code.
324</p>
325</dd>
326<dt><span>&lsquo;<samp>bootstrap-hwasan</samp>&rsquo;</span></dt>
327<dd><p>Compiles GCC itself using HWAddress Sanitization in order to catch invalid
328memory accesses within the GCC code.  This option is only available on AArch64
329systems that are running Linux kernel version 5.4 or later.
330</p>
331</dd>
332</dl>
333
334</div>
335<div class="section" id="Building-a-cross-compiler">
336<h3 class="section">Building a cross compiler</h3>
337
338<p>When building a cross compiler, it is not generally possible to do a
3393-stage bootstrap of the compiler.  This makes for an interesting problem
340as parts of GCC can only be built with GCC.
341</p>
342<p>To build a cross compiler, we recommend first building and installing a
343native compiler.  You can then use the native GCC compiler to build the
344cross compiler.  The installed native compiler needs to be GCC version
3452.95 or later.
346</p>
347<p>Assuming you have already installed a native copy of GCC and configured
348your cross compiler, issue the command <code>make</code>, which performs the
349following steps:
350</p>
351<ul>
352<li> Build host tools necessary to build the compiler.
353
354</li><li> Build target tools for use by the compiler such as binutils (bfd,
355binutils, gas, gprof, ld, and opcodes)
356if they have been individually linked or moved into the top level GCC source
357tree before configuring.
358
359</li><li> Build the compiler (single stage only).
360
361</li><li> Build runtime libraries using the compiler from the previous step.
362</li></ul>
363
364<p>Note that if an error occurs in any step the make process will exit.
365</p>
366<p>If you are not building GNU binutils in the same source tree as GCC,
367you will need a cross-assembler and cross-linker installed before
368configuring GCC.  Put them in the directory
369<samp><var>prefix</var>/<var>target</var>/bin</samp>.  Here is a table of the tools
370you should put in this directory:
371</p>
372<dl compact="compact">
373<dt><span><samp>as</samp></span></dt>
374<dd><p>This should be the cross-assembler.
375</p>
376</dd>
377<dt><span><samp>ld</samp></span></dt>
378<dd><p>This should be the cross-linker.
379</p>
380</dd>
381<dt><span><samp>ar</samp></span></dt>
382<dd><p>This should be the cross-archiver: a program which can manipulate
383archive files (linker libraries) in the target machine&rsquo;s format.
384</p>
385</dd>
386<dt><span><samp>ranlib</samp></span></dt>
387<dd><p>This should be a program to construct a symbol table in an archive file.
388</p></dd>
389</dl>
390
391<p>The installation of GCC will find these programs in that directory,
392and copy or link them to the proper place to for the cross-compiler to
393find them when run later.
394</p>
395<p>The easiest way to provide these files is to build the Binutils package.
396Configure it with the same <samp>--host</samp> and <samp>--target</samp>
397options that you use for configuring GCC, then build and install
398them.  They install their executables automatically into the proper
399directory.  Alas, they do not support all the targets that GCC
400supports.
401</p>
402<p>If you are not building a C library in the same source tree as GCC,
403you should also provide the target libraries and headers before
404configuring GCC, specifying the directories with
405<samp>--with-sysroot</samp> or <samp>--with-headers</samp> and
406<samp>--with-libs</samp>.  Many targets also require &ldquo;start files&rdquo; such
407as <samp>crt0.o</samp> and
408<samp>crtn.o</samp> which are linked into each executable.  There may be several
409alternatives for <samp>crt0.o</samp>, for use with profiling or other
410compilation options.  Check your target&rsquo;s definition of
411<code>STARTFILE_SPEC</code> to find out what start files it uses.
412</p>
413</div>
414<div class="section" id="Building-in-parallel">
415<h3 class="section">Building in parallel</h3>
416
417<p>GNU Make 3.80 and above, which is necessary to build GCC, support
418building in parallel.  To activate this, you can use &lsquo;<samp>make -j 2</samp>&rsquo;
419instead of &lsquo;<samp>make</samp>&rsquo;.  You can also specify a bigger number, and
420in most cases using a value greater than the number of processors in
421your machine will result in fewer and shorter I/O latency hits, thus
422improving overall throughput; this is especially true for slow drives
423and network filesystems.
424</p>
425</div>
426<div class="section" id="Building-the-Ada-compiler">
427<h3 class="section">Building the Ada compiler</h3>
428
429<p><a href="prerequisites.html#GNAT-prerequisite">GNAT prerequisites</a>.
430</p>
431</div>
432<div class="section" id="Building-the-D-compiler">
433<h3 class="section">Building the D compiler</h3>
434
435<p><a href="prerequisites.html#GDC-prerequisite">GDC prerequisites</a>.
436</p>
437</div>
438<div class="section" id="Building-with-profile-feedback">
439<h3 class="section">Building with profile feedback</h3>
440
441<p>It is possible to use profile feedback to optimize the compiler itself.  This
442should result in a faster compiler binary.  Experiments done on x86 using gcc
4433.3 showed approximately 7 percent speedup on compiling C programs.  To
444bootstrap the compiler with profile feedback, use <code>make profiledbootstrap</code>.
445</p>
446<p>When &lsquo;<samp>make profiledbootstrap</samp>&rsquo; is run, it will first build a <code>stage1</code>
447compiler.  This compiler is used to build a <code>stageprofile</code> compiler
448instrumented to collect execution counts of instruction and branch
449probabilities.  Training run is done by building <code>stagetrain</code>
450compiler.  Finally a <code>stagefeedback</code> compiler is built
451using the information collected.
452</p>
453<p>Unlike standard bootstrap, several additional restrictions apply.  The
454compiler used to build <code>stage1</code> needs to support a 64-bit integral type.
455It is recommended to only use GCC for this.
456</p>
457<p>On Linux/x86_64 hosts with some restrictions (no virtualization) it is
458also possible to do autofdo build with &lsquo;<samp>make
459autoprofiledback</samp>&rsquo;. This uses Linux perf to sample branches in the
460binary and then rebuild it with feedback derived from the profile.
461Linux perf and the <code>autofdo</code> toolkit needs to be installed for
462this.
463</p>
464<p>Only the profile from the current build is used, so when an error
465occurs it is recommended to clean before restarting. Otherwise
466the code quality may be much worse.
467</p>
468<hr />
469<p>
470<p><a href="./index.html">Return to the GCC Installation page</a>
471</p>
472
473
474
475
476
477
478<hr></div>
479
480
481
482</body>
483</html>
484