当前位置:首页 > 服务端 > Building GCC from source

Building GCC from source

Building GCC from source

  • Redhat/Centos 6 — GCC 4.4 (C++03)
  • Suse 12 — GCC 4.8 (experimental C++11)
  • Suse 11 — GCC 4.3 (C++03)
  • Ubuntu 18.04 — GCC 7.4 (full C++14; experimental C++17)

Overview

Building GCC from source involves the following sequence of steps:

  1. obtain source tarballs and unpack into appropriate directories
  2. create a clean build environment
  3. configure the source code
  4. compile the source code
  5. install the built artefacts
  6. usage

Choosing a GCC version

Usually we want to build the latest version of GCC (which is 9.1.0 as of mid 2019) so that we can access the latest C/C++ language features and compiler improvements. However the approach described here generally applies to earlier versions of GCC, and will likely work for later ones also.

gcc_version=9.1.0

Versions of dependencies

GCC depends on several other open source projects. These are typically support libraries, packaged separately to the GCC source code, that must also be downloaded and built. The list of required projects and recommended minimum versions can be found on the GCC prerequisites web page, although the version numbers listed there are often just a guide; in practice it might be required to bump the numbers slightly to get everything successfully working together.

gmp_version=6.1.2
mpfr_version=3.1.4
mpc_version=1.0.3
isl_version=0.18

Download

Each source code package should be obtained from a valid distribution origin, ideally from its project website. The build script defines a helper function called __wget which performs the download using the wget GNU/Linux utility; it is called for each package in turn:

__wget https://gmplib.org/download/gmp              $gmp_tarfile
__wget https://ftp.gnu.org/gnu/mpfr $mpfr_tarfile
__wget http://www.multiprecision.org/downloads $mpc_tarfile
__wget ftp://gcc.gnu.org/pub/gcc/infrastructure $isl_tarfile
__wget ftp://ftp.gnu.org/gnu/gcc/gcc-${gcc_version} $gcc_tarfile

Unpack

GCC’s build system has a helpful feature for building these dependencies. If the source code for a dependency is placed within the GCC source code folder, it will be automatically discovered and compiled during the main build of GCC. This is the approach recommended by the GCC website and adopted in the build script (an alternative approach is to manually build and install each dependency separately and then configure GCC to find them).

__untar "$source_dir/gcc-${gcc_version}" "$tarfile_dir/$mpfr_tarfile"mv -v $source_dir/gcc-${gcc_version}/mpfr-${mpfr_version} $source_dir/gcc-${gcc_version}/mpfr

Clean your shell

An important consideration when building any C/C++ project is to clean and control the shell environment. Environment variables present during compilation can have significant side effects on the build process, possibly causing compile or link failures, and later run-time errors when the built artefacts are used. So before proceeding to the configure and build steps we must first obtain a clean shell environment.

for i in $(env | awk -F"=" '{print $1}') ; do
unset $i || true # ignore unset fails
done

Configuration

After the source code packages have been unzipped they must next be configured.

install_dir=${HOME}/opt/gcc-${gcc_version}

Build

After a successful configure step we are now ready to build. Ensure you have around 5 GB of disk space free. Prefer to build on a local disk rather than a network share, for faster completion.

# make_flags="-j 2"

Install

After the build and optional checks have successfully completed, the next step is to install the built artefacts into their destination location. This location is specified via the --prefix option provided during the configuration step.

Usage

Assuming no errors, the compiler is now installed and can be invoked by providing its full path. For example, if it was installed at ~/opt/gcc-9.1.0 we can run:

$ ~/opt/gcc-9.1.0/bin/g++
centos7> ~/opt/gcc-9.1.0/bin/g++ --std=c++17 demo.cc -o demo
centos7> ./demo./demo: /lib64/libstdc++.so.6: version `CXXABI_1.3.9' not found (required by ./demo)./demo: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by ./demo)
centos7> export LD_LIBRARY_PATH=$HOME/opt/gcc-9.1.0/lib64centos7> ./demo
centos7> ~/opt/gcc-9.1.0/bin/g++ -Wl,-rpath=$HOME/opt/gcc-9.1.0/lib64 --std=c++17 demo.cc -o demo
centos7> ~/opt/gcc-9.1.0/bin/g++ --std=c++17 demo.cc -o demo -static-libstdc++ -static-libgcc
  • update LD_LIBRARY_PATH to find the new libraries
  • update MANPATH to find the new man-pages
$ source $HOME/opt/gcc-9.1.0/activate
$ man g++$ g++ --std=c++17 demo.cc -o demo$ ./demo

Summary

If you have followed these steps, or just run the build script, you should now have a production ready build of the latest version of GCC, and with it access to the latest C++ 17/2x language features.

作者:huorexiaji
来源链接:https://www.cnblogs.com/tju1895/p/16624211.html

版权声明:
1、Java侠(https://www.javaxia.com)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。

2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。





本文链接:https://www.javaxia.com/server/125600.html

标签: No compiler
分享给朋友: