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.

posted @   huorexiaji  阅读(110)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示