This article is generated by Tencent Yuanbao, with content outline and verification provided by @Xiao Hui~
In certain Linux environments, the system’s default GCC version is relatively low (e.g., 4.8.5), which cannot meet the compilation requirements of new software, and it is not possible to install a newer version of GCC directly through the package manager. In this case, compiling and installing GCC from source becomes a necessary option. This article will detail the process of compiling and installing GCC 7.5.0 from source, as well as its configuration and usage in a multi-version environment.
1. Compiling and Installing GCC 7.5.0 from Source
1.1 Preparation
Create an installation directory and download the GCC 7.5.0 source package:
cd /opt && mkdir gcc-7.5.0
wget https://ftp.gnu.org/gnu/gcc/gcc-7.5.0/gcc-7.5.0.tar.xz
tar -xf gcc-7.5.0.tar.xz
cd gcc-7.5.0
1.2 Resolving Dependencies
The compilation of GCC depends on three mathematical libraries: GMP, MPFR, and MPC. Running configure directly usually results in an error:
./configure --prefix=/opt/gcc-7.5.0 --enable-checking=release --enable-languages=c,c++ --disable-multilib
Error message:<span>configure: error: Building GCC requires GMP 4.2+, MPFR 2.4.0+ and MPC 0.8.0+</span>
Manually install the dependency libraries:
-
Install necessary tools:
yum install bzip2 yum install m4 -
Download and compile the dependency libraries: Download the required versions of the dependency libraries from
<span>https://gcc.gnu.org/pub/gcc/infrastructure/</span>:# Compile and install GMP tar -jxvf gmp-6.1.0.tar.bz2 cd gmp-6.1.0 ./configure make make install cd .. # Compile and install MPFR tar -jxvf mpfr-3.1.4.tar.bz2 cd mpfr-3.1.4 ./configure make make install cd .. # Compile and install MPC tar -zxvf mpc-1.0.3.tar.gz cd mpc-1.0.3 ./configure make make install cd ..The installation order of the dependency libraries is crucial; GMP must be installed first, followed by MPFR, and finally MPC, due to their interdependencies.
1.3 Compiling and Installing GCC
After completing the installation of the dependency libraries, reconfigure and compile GCC:
# Configure GCC compilation options
./configure --prefix=/opt/gcc-7.5.0 --enable-checking=release --enable-languages=c,c++ --disable-multilib
# Use multi-threaded compilation to speed up the process (using 16 threads here)
make -j16
# Install GCC to the specified directory
make install
The compilation process can be time-consuming, depending on the machine’s performance.
1.4 Setting Environment Variables
Add the newly installed GCC to the PATH environment variable and set the runtime library search path:
export PATH=/opt/gcc-7.5.0/bin:$PATH
export LD_LIBRARY_PATH=/opt/gcc-7.5.0/lib64:$LD_LIBRARY_PATH
1.5 Verifying Installation
Check the versions of GCC and G++ to confirm successful installation:
gcc --version
g++ --version
The compiled GCC 7.5.0 library files are located in the<span>/opt/gcc-7.5.0/lib64</span>directory.
2. Using GCC 7.5.0 in a GCC 4.8.5 Environment
2.1 Temporarily Using the New Version of GCC
In the current session, temporarily switch the GCC version by setting the environment variables:
export PATH=/opt/gcc-7.5.0/bin:$PATH
export LD_LIBRARY_PATH=/opt/gcc-7.5.0/lib64:$LD_LIBRARY_PATH
After setting, the<span>gcc</span> and <span>g++</span> commands in the current session will use version 7.5.0.
2.2 Compiling Programs
Use GCC 7.5.0 to compile programs, and the compiler will link to the new version of the library files:
gcc -o your_program your_source.c
2.3 Checking Program Dependency Libraries
Use the<span>ldd</span> command to check the library file paths that the compiled program depends on:
ldd your_program | grep opt
The output should show that the program is linked to the libraries of GCC 7.5.0:
libstdc++.so.6 => /opt/gcc-7.5.0/lib64/libstdc++.so.6 (0x00007ff208873000)
libgcc_s.so.1 => /opt/gcc-7.5.0/lib64/libgcc_s.so.1 (0x00007ff20835a000)
2.4 Running Programs on Other Machines
To run programs compiled with GCC 7.5.0 on other machines, you need to copy the required shared libraries (such as<span>libstdc++.so.6</span> and <span>libgcc_s.so.1</span>) from the<span>/opt/gcc-7.5.0/lib64/</span> directory to the library directory of the target machine (such as<span>/usr/lib64/</span> or the <span>lib/</span> subdirectory of the program’s directory), or set the target machine’s <span>LD_LIBRARY_PATH</span> environment variable to point to the directory containing these libraries.
Notes
- Dependency Library Versions: Ensure that the versions of GMP, MPFR, and MPC are compatible with GCC 7.5.0. Using the recommended versions from the official infrastructure directory is the safest choice.
- Persisting Environment Variables: If you want to permanently use GCC 7.5.0, you can add
<span>export PATH=/opt/gcc-7.5.0/bin:$PATH</span>and<span>export LD_LIBRARY_PATH=/opt/gcc-7.5.0/lib64:$LD_LIBRARY_PATH</span>to the user’s shell configuration file (such as<span>~/.bashrc</span>or<span>~/.bash_profile</span>). - Permission Issues: If installing in an environment without root privileges, you need to use
<span>--prefix</span>to specify a writable installation path during configure. - Library Conflicts: Be cautious when copying library files to avoid overwriting system libraries, which may lead to dependency issues for system software.
By following the above steps, you can successfully install and use GCC 7.5.0 to compile and run programs that require features from the new version, while retaining the original GCC 4.8.5 in the system.