<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sect1 PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
  "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
  <!ENTITY % general-entities SYSTEM "../general.ent">
  %general-entities;
]>

<sect1 id="ch-system-gcc" role="wrap">
  <?dbhtml filename="gcc.html"?>

  <sect1info condition="script">
    <productname>gcc</productname>
    <productnumber>&gcc-version;</productnumber>
    <address>&gcc-url;</address>
  </sect1info>

  <title>GCC-&gcc-version;</title>

  <indexterm zone="ch-system-gcc">
    <primary sortas="a-GCC">GCC</primary>
  </indexterm>

  <sect2 role="package">
    <title/>

    <para>GCC 软件包包含 GNU 编译器集合，其中有 C 和 C++ 编译器。</para>

    <segmentedlist>
      <segtitle>&buildtime;</segtitle>
      <segtitle>&diskspace;</segtitle>

      <seglistitem>
        <seg>&gcc-ch6-sbu;</seg>
        <seg>&gcc-ch6-du;</seg>
      </seglistitem>
    </segmentedlist>

  </sect2>

  <sect2 role="installation">
    <title>安装 GCC</title>

    <para>在 x86_64 上构建时，修改存放 64 位库的默认路径为
    	<quote>lib</quote>:</para>

<screen><userinput remap="pre">case $(uname -m) in
  x86_64)
    sed -e '/m64=/s/lib64/lib/' \
        -i.orig gcc/config/i386/t-linux64
  ;;
esac</userinput></screen>

    <para>删除之前创建的符号链接，以便安装最终的 GCC 头文件：</para>

<screen><userinput remap="pre">rm -f /usr/lib/gcc</userinput></screen>

    <para>GCC 文档推荐在专用的构建目录中构建 GCC：</para>

<screen><userinput remap="pre">mkdir -v build
cd       build</userinput></screen>

    <para>准备安装 GCC：</para>

<screen><userinput remap="configure">SED=sed                               \
../configure --prefix=/usr            \
             --enable-languages=c,c++ \
             --disable-multilib       \
             --disable-bootstrap      \
             --with-system-zlib</userinput></screen>

    <!--para>Note that for other languages, there are some prerequisites that
    are not yet available. See the
    <ulink url="&blfs-book;general/gcc.html">BLFS Book</ulink>
    for instructions on how to build all of GCC's supported languages.</para-->
    <para>请注意，对于其他语言，还有一些尚未满足的依赖项。
		阅读<ulink url="&blfs-book;general/gcc.html">BLFS 手册</ulink>，
		以了解如何构建 GCC 支持的所有语言。</para>

    <variablelist>
      <title>新的配置选项的含义：</title>

      <varlistentry>
        <term><envar>SED=sed</envar></term>
        <listitem>
          <para>设定该环境变量，防止路径 /tools/bin/sed
          	  被硬编码到编译结果中。</para>
        </listitem>
      </varlistentry>
 
      <varlistentry>
        <term><parameter>--with-system-zlib</parameter></term>
        <listitem>
          <para>该选项使得 GCC 链接到系统安装的 Zlib 库，而不是它自带的
          Zlib 副本。</para>
        </listitem>
      </varlistentry>
    </variablelist>

    <para>编译该软件包：</para>

<screen><userinput remap="make">make</userinput></screen>

    <important>
      <para>本节中 GCC 的测试套件被认为是关键的，无论如何不能跳过。</para>
    </important>

   <para>已知 GCC 测试套件中的一组测试可能耗尽栈空间，
   因此运行测试前要增加栈空间：</para>

<screen><userinput remap="test">ulimit -s 32768</userinput></screen>

    <para>测试编译结果，但出错时继续执行其他测试：</para>

<screen><userinput remap="test">make -k check</userinput></screen>

    <para>输入以下命令查看测试结果的摘要：</para>

<screen><userinput remap="test">../contrib/test_summary</userinput></screen>

    <para>如果只想看摘要，将输出用管道送至
    <userinput>grep -A7 Summ</userinput>。</para>

    <para>可以将结果与 <ulink
    url="&test-results;"/> 和
    <ulink url="https://gcc.gnu.org/ml/gcc-testresults/"/>
    的结果进行比较。</para>

    <!--para>A few unexpected failures cannot always be avoided. The GCC developers
    are usually aware of these issues, but have not resolved them yet.
    In particular, six tests in the libstdc++ test suite are known to fail
    when running as the root user as we do here.
    Unless the test results are vastly different from those at the above URL,
    it is safe to continue.</para-->
    <para>少量意外的失败有时无法避免，GCC 开发者一般知道这类问题，
		但尚未解决它们。特别地， libstdc++ 测试套件中有 6 个测试在以
		root 身份运行时（我们现在就是这样做）会失败。
		我们可以继续安全地构建系统，除非测试结果和以上 URL 的结果截然不同。
	</para>

    <note><para>
      <!--On some combinations of kernel configuration and AMD processors
      there may be more than 1100 failures in the gcc.target/i386/mpx
      tests (which are designed to test the MPX option on recent
      Intel processors). These can safely be ignored on AMD
      processors. These tests will also fail on Intel processors if MPX support
	  is not enabled in the kernel even though it is present on the CPU.-->
	  在某些 AMD 处理器上，一些特定的内核配置可以导致 gcc.target/i386/mpx
	  中的 1100 多个测试失败（它们被设计为在较新的 Intel 处理器上测试 MPX
	  选项）。在 AMD 处理器上可以安全地忽略这些失败。
	  另外，如果在内核配置中没有启用 MPX，即使在支持 MPX 的 Intel CPU 上，
	  这些测试也会失败。
    </para></note>

    <para>安装该软件包：</para>

<screen><userinput remap="install">make install</userinput></screen>

    <para>创建一个 <ulink
    url="https://refspecs.linuxfoundation.org/FHS_3.0/fhs/ch03s09.html">FHS</ulink>
    因 <quote>历史原因</quote> 要求的符号链接：</para>

<screen><userinput remap="install">ln -sv ../usr/bin/cpp /lib</userinput></screen>

    <para>许多软件包使用 <command>cc</command> 调用 C 编译器。为了满足它们，
    创建一个符号链接：</para>

<screen><userinput remap="install">ln -sv gcc /usr/bin/cc</userinput></screen>

    <para>创建一个兼容性符号链接，以支持在构建程序时使用链接时优化（LTO）：
    </para> 

<screen><userinput remap="install">install -v -dm755 /usr/lib/bfd-plugins
ln -sfv ../../libexec/gcc/$(gcc -dumpmachine)/&gcc-version;/liblto_plugin.so \
        /usr/lib/bfd-plugins/</userinput></screen>

    <para>现在最终的工具链已经就位，
		重要的是再次确认编译和链接像我们期望的一样正常工作。
		再一次执行之前进行过的完整性检查：
	</para>

    <xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
    href="adjusting.xml"
    xpointer="xpointer(//*[@os='a'])"/>

    <xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
    href="adjusting.xml"
    xpointer="xpointer(//*[@os='b'])"/>

    <xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
    href="adjusting.xml"
    xpointer="xpointer(//*[@os='c'])"/>

    <xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
    href="adjusting.xml"
    xpointer="xpointer(//*[@os='d'])"/>

    <xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
    href="adjusting.xml"
    xpointer="xpointer(//*[@os='e'])"/>

    <xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
    href="adjusting.xml"
    xpointer="xpointer(//*[@os='f'])"/>

<screen><computeroutput>/usr/lib/gcc/x86_64-pc-linux-gnu/&gcc-version;/../../../../lib/crt1.o succeeded
/usr/lib/gcc/x86_64-pc-linux-gnu/&gcc-version;/../../../../lib/crti.o succeeded
/usr/lib/gcc/x86_64-pc-linux-gnu/&gcc-version;/../../../../lib/crtn.o succeeded</computeroutput></screen>

  <!--para>Depending on your machine architecture, the above may differ slightly,
  the difference usually being the name of the directory
  after <filename class="directory">/usr/lib/gcc</filename>. The important
  thing to look for here is that <command>gcc</command> has found all three
  <filename>crt*.o</filename> files under the
  <filename class="directory">/usr/lib</filename> directory.</para-->
  <para>以上结果可能随您的机器体系结构不同而略微不同，
	  区别一般是 <filename class="directory">/usr/lib/gcc</filename>
	  之后的目录名。我们关注的重点是， <command>gcc</command> 应该在
	  <filename class="directory">/usr/lib</filename> 目录中找到所有三个
	  <filename>crt*.o</filename> 文件。</para>

    <xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
    href="adjusting.xml"
    xpointer="xpointer(//*[@os='g'])"/>

<screen><userinput>grep -B4 '^ /usr/include' dummy.log</userinput></screen>

    <xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
    href="adjusting.xml"
    xpointer="xpointer(//*[@os='h'])"/>

<screen><computeroutput>#include &lt;...&gt; search starts here:
 /usr/lib/gcc/x86_64-pc-linux-gnu/&gcc-version;/include
 /usr/local/include
 /usr/lib/gcc/x86_64-pc-linux-gnu/&gcc-version;/include-fixed
 /usr/include</computeroutput></screen>

   <!--para>Again, note that the directory named after your target triplet may be
   different than the above, depending on your architecture.</para-->
   <para>同样要注意以您的目标三元组命名的目录根据您体系结构的不同，
	   可能和以上不同。</para>

<!--  This appears to be obsolete

    <note><para>As of version 4.3.0, GCC now unconditionally installs the
    <filename>limits.h</filename> file into the private
    <filename class="directory">include-fixed</filename> directory, and that
    directory is required to be in place.</para></note>
-->

    <xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
    href="adjusting.xml"
    xpointer="xpointer(//*[@os='i'])"/>

    <xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
    href="adjusting.xml"
    xpointer="xpointer(//*[@os='j'])"/>

    <xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
    href="adjusting.xml"
    xpointer="xpointer(//*[@os='k'])"/>

<screen><computeroutput>SEARCH_DIR("/usr/x86_64-pc-linux-gnu/lib64")
SEARCH_DIR("/usr/local/lib64")
SEARCH_DIR("/lib64")
SEARCH_DIR("/usr/lib64")
SEARCH_DIR("/usr/x86_64-pc-linux-gnu/lib")
SEARCH_DIR("/usr/local/lib")
SEARCH_DIR("/lib")
SEARCH_DIR("/usr/lib");</computeroutput></screen>

   <para>在 32 位系统上可能显示一些不同的目录，例如下面是 i686
   	   机器上的输出：</para>

<screen><computeroutput>SEARCH_DIR("/usr/i686-pc-linux-gnu/lib32")
SEARCH_DIR("/usr/local/lib32")
SEARCH_DIR("/lib32")
SEARCH_DIR("/usr/lib32")
SEARCH_DIR("/usr/i686-pc-linux-gnu/lib")
SEARCH_DIR("/usr/local/lib")
SEARCH_DIR("/lib")
SEARCH_DIR("/usr/lib");</computeroutput></screen>

    <xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
    href="adjusting.xml"
    xpointer="xpointer(//*[@os='l'])"/>

    <xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
    href="adjusting.xml"
    xpointer="xpointer(//*[@os='m'])"/>

    <xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
    href="adjusting.xml"
    xpointer="xpointer(//*[@os='n'])"/>

    <xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
    href="adjusting.xml"
    xpointer="xpointer(//*[@os='o'])"/>

    <xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
    href="adjusting.xml"
    xpointer="xpointer(//*[@os='p'])"/>

    <xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
    href="adjusting.xml"
    xpointer="xpointer(//*[@os='q'])"/>

    <xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
    href="adjusting.xml"
    xpointer="xpointer(//*[@os='r'])"/>

    <xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
    href="adjusting.xml"
    xpointer="xpointer(//*[@os='s'])"/>

    <xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
    href="adjusting.xml"
    xpointer="xpointer(//*[@os='t'])"/>

    <xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
    href="adjusting.xml"
    xpointer="xpointer(//*[@os='u'])"/>

    <xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
    href="adjusting.xml"
    xpointer="xpointer(//*[@os='v'])"/>

    <para>最后移动一个位置不正确的文件：</para>

<screen><userinput remap="install">mkdir -pv /usr/share/gdb/auto-load/usr/lib
mv -v /usr/lib/*gdb.py /usr/share/gdb/auto-load/usr/lib</userinput></screen>

  </sect2>

  <sect2 id="contents-gcc" role="content">
    <title>GCC 的内容</title>

    <segmentedlist>
      <segtitle>安装的程序</segtitle>
      <segtitle>安装的库</segtitle>
      <segtitle>安装的目录</segtitle>

      <seglistitem>
        <seg>c++, cc (到 gcc 的链接), cpp, g++, gcc,
        gcc-ar, gcc-nm, gcc-ranlib, 以及 gcov</seg>

        <seg>libasan.{a,so}, libatomic.{a,so}, libgcc.a, libgcc_eh.a, 
        libgcc_s.so, libgcov.a, libgomp.{a,so}, libiberty.a, libitm.{a,so},
        liblto_plugin.so, 
        libquadmath.{a,so}, libssp.{a,so},
        libssp_nonshared.a, libstdc++.{a,so}, libsupc++.a, 以及 libtsan.{a,so}</seg>

        <seg>/usr/include/c++, /usr/lib/gcc, /usr/libexec/gcc, 以及
        /usr/share/gcc-&gcc-version;</seg>
      </seglistitem>
    </segmentedlist>

    <variablelist>
      <bridgehead renderas="sect3">简要描述</bridgehead>
      <?dbfo list-presentation="list"?>
      <?dbhtml list-presentation="table"?>

      <varlistentry id="c">
        <term><command>c++</command></term>
        <listitem>
          <para>C++ 编译器</para>
          <indexterm zone="ch-system-gcc c">
            <primary sortas="b-c++">c++</primary>
          </indexterm>
        </listitem>
      </varlistentry>

      <varlistentry id="cc">
        <term><command>cc</command></term>
        <listitem>
          <para>C 编译器</para>
          <indexterm zone="ch-system-gcc cc">
            <primary sortas="b-cc">cc</primary>
          </indexterm>
        </listitem>
      </varlistentry>

      <varlistentry id="cpp">
        <term><command>cpp</command></term>
        <listitem>
          <para>C 预处理器，编译器使用它展开源文件中的 #include、#define
          及类似指令</para>
          <indexterm zone="ch-system-gcc cpp">
            <primary sortas="b-cpp">cpp</primary>
          </indexterm>
        </listitem>
      </varlistentry>

      <varlistentry id="g">
        <term><command>g++</command></term>
        <listitem>
          <para>C++ 编译器</para>
          <indexterm zone="ch-system-gcc g">
            <primary sortas="b-g++">g++</primary>
          </indexterm>
        </listitem>
      </varlistentry>

      <varlistentry id="gcc">
        <term><command>gcc</command></term>
        <listitem>
          <para>C 编译器</para>
          <indexterm zone="ch-system-gcc gcc">
            <primary sortas="b-gcc">gcc</primary>
          </indexterm>
        </listitem>
      </varlistentry>

      <varlistentry id="gcc-ar">
        <term><command>gcc-ar</command></term>
        <listitem>
          <!--para>A wrapper around <command>ar</command> that adds a
          plugin to the command line. This program is only used
          to add "link time optimization" and is not useful with the
          default build options</para-->
	      <para><command>ar</command> 的一个包装器，
			  它在命令行中添加一个插件。这个程序只被用于提供链接时优化功能，
			  对于默认的构建选项来说没有作用。</para>
          <indexterm zone="ch-system-gcc gcc-ar">
            <primary sortas="b-gcc-ar">gc-ar</primary>
          </indexterm>
        </listitem>
      </varlistentry>

      <varlistentry id="gcc-nm">
        <term><command>gcc-nm</command></term>
        <listitem>
          <!--para>A wrapper around <command>nm</command> that adds a
          plugin to the command line. This program is only used
          to add "link time optimization" and is not useful with the
          default build options</para-->
	      <para><command>nm</command> 的一个包装器，
			  它在命令行中添加一个插件。这个程序只被用于提供链接时优化功能，
			  对于默认的构建选项来说没有作用。</para>
          <indexterm zone="ch-system-gcc gcc-nm">
            <primary sortas="b-gcc-nm">gc-nm</primary>
          </indexterm>
        </listitem>
      </varlistentry>

      <varlistentry id="gcc-ranlib">
        <term><command>gcc-ranlib</command></term>
        <listitem>
          <!--para>A wrapper around <command>ranlib</command> that adds a
          plugin to the command line. This program is only used
          to add "link time optimization" and is not useful with the
          default build options</para-->
	      <para><command>ranlib</command> 的一个包装器，
			  它在命令行中添加一个插件。这个程序只被用于提供链接时优化功能，
			  对于默认的构建选项来说没有作用。</para>
          <indexterm zone="ch-system-gcc gcc-ranlib">
            <primary sortas="b-gcc-ranlib">gc-ranlib</primary>
          </indexterm>
        </listitem>
      </varlistentry>

      <varlistentry id="gcov">
        <term><command>gcov</command></term>
        <listitem>
		  <para>一个覆盖率测试工具；
			  用于分析程序并确定在哪里优化最有效</para>
          <indexterm zone="ch-system-gcc gcov">
            <primary sortas="b-gcov">gcov</primary>
          </indexterm>
        </listitem>
      </varlistentry>

      <varlistentry id="libasan">
        <term><filename class="libraryfile">libasan</filename></term>
        <listitem>
          <para>地址完整性检查库</para>
          <indexterm zone="ch-system-gcc libasan">
            <primary sortas="b-libasan">libasan</primary>
          </indexterm>
        </listitem>
      </varlistentry>

      <varlistentry id="libgcc">
        <term><filename class="libraryfile">libgcc</filename></term>
        <listitem>
          <para>包含 <command>gcc</command> 的运行时支持</para>
          <indexterm zone="ch-system-gcc libgcc">
            <primary sortas="c-libgcc">libgcc</primary>
          </indexterm>
        </listitem>
      </varlistentry>

      <varlistentry id="libgcov">
        <term><filename class="libraryfile">libgcov</filename></term>
        <listitem>
          <para>在 GCC 被指示启动性能剖析时，这个库被链接到程序中</para>
          <indexterm zone="ch-system-gcc libgcov">
            <primary sortas="c-libgcov">libgcov</primary>
          </indexterm>
        </listitem>
      </varlistentry>

      <varlistentry id="libgomp">
        <term><filename class="libraryfile">libgomp</filename></term>
        <listitem>
          <para>OpenMP API 的 GNU 实现，用于 C/C++ 和 Fortran
          	  的跨平台共享内存并行编程</para>
          <indexterm zone="ch-system-gcc libgomp">
            <primary sortas="c-libgomp">libgomp</primary>
          </indexterm>
        </listitem>
      </varlistentry>

      <varlistentry id="libiberty">
        <term><filename class="libraryfile">libiberty</filename></term>
        <listitem>
          <para>包含若干 GNU 程序使用的子程序，如 
          <command>getopt</command>, <command>obstack</command>,
          <command>strerror</command>, <command>strtol</command>, and
          <command>strtoul</command></para>
          <indexterm zone="ch-system-gcc libiberty">
            <primary sortas="c-libiberty">libiberty</primary>
          </indexterm>
        </listitem>
      </varlistentry>

      <varlistentry id="liblto_plugin">
        <term><filename class="libraryfile">liblto_plugin</filename></term>
        <listitem>
		  <para>GCC 的链接时优化 (LTO) 插件，
			  使得 GCC 可以进行跨越编译单元的优化</para>
          <indexterm zone="ch-system-gcc liblto_plugin">
            <primary sortas="c-liblto_plugin">liblto_plugin</primary>
          </indexterm>
        </listitem>
      </varlistentry>

      <varlistentry id="libquadmath">
        <term><filename class="libraryfile">libquadmath</filename></term>
        <listitem>
          <para>GCC 四精度数学 API 库</para>
          <indexterm zone="ch-system-gcc libquadmath">
            <primary sortas="c-libquadmath">libquadmath</primary>
          </indexterm>
        </listitem>
      </varlistentry>

      <varlistentry id="libssp">
        <term><filename class="libraryfile">libssp</filename></term>
        <listitem>
          <para>包含 GCC 的栈溢出保护功能支持子程序</para>
          <indexterm zone="ch-system-gcc libssp">
            <primary sortas="c-libssp">libssp</primary>
          </indexterm>
        </listitem>
      </varlistentry>

      <varlistentry id="libstdc">
        <term><filename class="libraryfile">libstdc++</filename></term>
        <listitem>
          <para>C++ 标准库</para>
          <indexterm zone="ch-system-gcc libstdc">
            <primary sortas="c-libstdc++">libstdc++</primary>
          </indexterm>
        </listitem>
      </varlistentry>

      <varlistentry id="libsupc">
        <term><filename class="libraryfile">libsupc++</filename></term>
        <listitem>
          <para>包含 C++ 编程语言支持子程序</para>
          <indexterm zone="ch-system-gcc libsupc">
            <primary sortas="c-libsupc++">libsupc++</primary>
          </indexterm>
        </listitem>
      </varlistentry>

      <varlistentry id="libtsan">
        <term><filename class="libraryfile">libtsan</filename></term>
        <listitem>
          <para>线程完整性检查库</para>
          <indexterm zone="ch-system-gcc libtsan">
            <primary sortas="c-libtsan">libtsan</primary>
          </indexterm>
        </listitem>
      </varlistentry>

    </variablelist>

  </sect2>

</sect1>
