[Ubuntu][GCC] Downloading and Compiling GCC Source Code

1. Download the GCC installation package. You can search for the official address to download it. If you cannot download it, you can try selecting a required GCC version from gitee.com/FIRC/gcc-cn-mirror. Note that if you are downloading the source code from gcc-cn-mirror, you can skip steps 2 and 3 and proceed directly to step 4 to start the compilation.

2. Download the dependency packages. Check the download_prerequisites file in the contrib folder of the downloaded GCC installation package to see the required versions of the dependency packages.

[Ubuntu][GCC] Downloading and Compiling GCC Source Code

Edit

Based on the information in the red box in download_prerequisites, download the corresponding versions of the dependency packages from the links below.

Dependency package download address: Find the corresponding address in the base_url of the source code and click to find the corresponding version installation package.

3. Compile GCC. The GCC version and dependency packages used in this article are as follows:

gcc-9.10 gmp-6.1.0 mpfr-3.1.4 mpc-1.0.3 isl-0.18

Copy the GCC-9.10 installation package to Linux and extract it.

tar -zxvf gcc-9.10.tar.gz

Enter the gcc-9.10 directory.

cd gcc-9.10

Copy the dependency packages to gcc-9.10 and extract them.

tar -zxvf gmp-6.1.0.tar.bz2

tar -zxvf mpfr-3.1.4.tar.bz2

tar -zxvf mpc-1.0.3.tar.gz

tar -zxvf isl-0.18.tar.bz2

Create symbolic links.

ln -sf gmp-6.1.0 gmp
ln -sf mpfr-3.1.4 mpfr
ln -sf mpc-1.0.3 mpc
ln -sf isl-0.18 isl

Compile GCC (the compilation time is approximately half an hour to one hour).

4. Create a build directory.

mkdir build && cd build

unset LIBRARY_PATH CPATH C_INCLUDE_PATH PKG_CONFIG_PATH CPLUS_INCLUDE_PATH INCLUDE

# Configuration

../configure --enable-checking=release --enable-languages=c,c++ --disable-multilib

# Compilation

make -j8

5. Installation. Since there is no root permission, you cannot install to /usr/local, so you need to specify another path.

# The following DESTDIR=/home is just an example; please choose your own installation path.

make install DESTDIR=/home

Configure environment variables.

# Create a new file <code>bashrc
vim bashrc
# Enter the following information

export PATH=/home/usr/local/bin:$PATH
export LD_LIBRARY_PATH=/home/usr/local/lib64:$LD_LIBRARY_PATH

# Save and exit: :wq

# Update environment variables (each time you open a shell, you need to execute the following command again).

. bashrc

or

source bashrc

Check the GCC version.

gcc --version

The following information will be displayed, indicating that the configuration is complete:

gcc (GCC) 9.1.0 Copyright (C) 2019 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

At this point, the offline upgrade of GCC on Linux is complete without root permissions. You can use the new version of GCC for compilation in the current shell.

Leave a Comment