首页 > 其他分享 >Building GCC from source

Building GCC from source

时间:2022-08-25 14:47:32浏览次数:89  
标签:Building GCC script gcc source version build

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.

标签:Building,GCC,script,gcc,source,version,build
From: https://www.cnblogs.com/tju1895/p/16624211.html

相关文章

  • WPF Textbox 控件绑定数据即时更新UpdateSourceTrigger=PropertyChanged
     Textbox控件绑定数据类型为doubie类型时,如果触发条件为UpdateSourceTrigger=PropertyChanged时,无法输入小数点。 解决方案:方法一:关闭数据一至检查System.......
  • gcc编译器的常用命令行参数
    在学习常用的gcc命令行参数前,先了解gcc在执行编译工作的过程1、预处理,生成.i的文件[预处理器cpp]2、将预处理后的文件转换成汇编语言,生成文件.s[编译器egcs]3......
  • GCC-GCC中的-Wl选项说明
    GCC-GCC中的-Wl选项说明在GCC编译程序时,由于GCC命令不经能够编译,也能够链接程序,GCC链接程序是通过ld命令实现的,如何将GCC的命令行参数传递给ld命令呢,这就是通过-Wl,来实现......
  • eureka 出现Failed to configure a DataSource: 'url' attribute is not specified an
    首选要声明一下SpringBoot版本和SpringCloud的版本要兼容的参考官网:https://spring.io/projects/spring-cloud#overview我使用的版本是springboot2.3.9.RELEASEsprin......
  • maven-resources-production:XXXXX: java.lang.IndexOutOfBoundsException 异常处理
     处理过程:1、IDEA设置是否开启自动编辑,File->Settings->Compiler->勾选Buildprojectautomatically2、build项目,直接报错报错:maven-resources-production:XXXX:java.......
  • C#任务取消--CancellationTokenSource取消令牌源
    一、概述在讲任务取消之前,得先了解取消令牌跟取消令牌源,实现任务取消功能的就是依靠这两个。CancellationTokenSource:取消令牌源类,拥有Cancel()方法,可以给关联的令牌......
  • .NET(C#) 读取Resource资源文件的方法
    System.Resources名字空间支持三种资源文件:.txt文件,只能有字符串资源。不能被嵌入到Assembly中,则很容易暴露,被客户修改。最大缺点是仅支持字符串资源,不推荐使用。.resx......
  • gcc编译器
    一、gcc,即GNUCCompile  gcc仅仅是一个编译器,没有界面,必须在命令行模式下使用。通过gcc命令可以将源文件编译成可执行文件。  gcc既可以一次性完成C语言源文件......
  • /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o: In function `
    原因:C语言的头文件不够错误代码:未导入#include<stdlib.h>报错#include<stdio.h>#defineR1intmain(){floatc,r,s;c=2;#ifRr=3.14*c*c;printf......
  • ubuntu下升级gcc11环境
    使用ppa源升级官网地址:https://launchpad.net/ppatoolchan/test地址:  https://launchpad.net/~ubuntu-toolchain-r/+archive/ubuntu/test 1.添加软件源......