Installing GCC 12 on CentOS 7: A Comprehensive Guide from Dependency Preparation to Compilation Verification

Installing GCC 12 on CentOS 7: A Comprehensive Guide from Dependency Preparation to Compilation Verification

As a “compilation tool” in Linux systems, GCC (GNU Compiler Collection) is a core tool for developing programs in C, C++, and more. However, the default GCC version that comes with CentOS 7 is relatively low (usually 4.8.5), which cannot meet the development requirements for C++17 and above standards. Today, we will guide you step by step to compile and install GCC 12 on CentOS 7 to resolve version compatibility issues.

1. Prerequisites: Preparing Dependencies and Environment

GCC 12 is not available in the default software repository of CentOS 7, so it needs to be installed via source compilation. Before compiling, you must install the dependency packages to avoid errors such as “missing files” or “unable to compile” later on.

1. Upgrade System Base Tools

First, update the installed software packages to ensure that the base tools are up to date:

sudo yum update -y

2. Install Compilation Dependencies

Compiling GCC requires dependencies such as <span>gcc</span> (the old version must also be retained for compiling the new version), <span>make</span>, <span>libmpc</span>, <span>mpfr</span>, <span>gmp</span>, and other tools and libraries, which can be installed directly via yum:

sudo yum install -y gcc make wget tar bzip2 glibc-devel glibc-devel.i686 libmpc-devel mpfr-devel gmp-devel
  • Explanation:<span>libmpc</span>, <span>mpfr</span>, and <span>gmp</span> are essential mathematical libraries for GCC compilation; <span>glibc-devel</span> contains the development files for the C standard library, ensuring that the compiled programs can run normally.

2. Download the GCC 12 Source Package

The official GCC source is hosted on the GNU server, and we will directly download the stable version (here we choose <span>gcc-12.2.0</span>, which is the long-term support stable version as of May 2024).

1. Switch to a Temporary Directory (to avoid confusion)

It is recommended to operate in the <span>/usr/local/src</span> directory, which is typically used for storing source files:

cd /usr/local/src

2. Download the Source Package

Use the <span>wget</span> command to download the GCC 12.2.0 source (if the speed is slow, you can copy the link to a browser to download and then upload it to the server):

sudo wget https://ftp.gnu.org/gnu/gcc/gcc-12.2.0/gcc-12.2.0.tar.gz

Alternatively, you can also use a domestic mirror source:


https://mirrors.ustc.edu.cn/gnu/gcc/gcc-12.2.0/gcc-12.2.0.tar.gz

3. Extract the Source Package

After downloading, use the <span>tar</span> command to extract it, obtaining the source directory:

sudo tar -zxvf gcc-12.2.0.tar.gz

After extraction, a <span>gcc-12.2.0</span> directory will be created, and subsequent compilation operations will be performed in this directory.

3. Compile and Install GCC 12: Key Steps to Avoid Pitfalls

The GCC compilation process consists of three steps: “configuration”, “compilation”, and “installation”, among which compilation takes a long time (depending on server performance, it may take 30 minutes to 2 hours), so please be patient.

1. Enter the Source Directory

cd gcc-12.2.0

2. Configure Compilation Parameters

Use the <span>./configure</span> command to specify the installation path and compilation options. The key parameters are as follows:

  • <span>--prefix=/usr/local/gcc-12</span>: Specifies the installation directory for GCC 12 (to avoid overwriting the system’s default GCC 4.8.5);
  • <span>--enable-languages=c,c++</span>: Enables support for C and C++ languages (if other languages are needed, you can add <span>fortran</span>, etc.);
  • <span>--disable-multilib</span>: Disables 32-bit library support (CentOS 7 64-bit system does not require 32-bit libraries, which can reduce compilation time).

Execute the configuration command:

sudo ./configure --prefix=/usr/local/gcc-12 --enable-languages=c,c++ --disable-multilib --enable-threads=posix
  • Note: If the configuration process prompts “missing a dependency”, you need to return to the “Prerequisites” step to confirm that the corresponding dependencies are installed.

3. Compile the Source Code

Use the <span>make</span> command to compile. To speed up the process, you can specify the number of threads for parallel compilation using the <span>-j</span> parameter (it is recommended to set it to the number of CPU cores on the server, e.g., <span>-j4</span> indicates 4 threads):

sudo make -j4
  • The compilation takes a long time, and please do not interrupt the terminal connection during this period (if connected via SSH, it is recommended to use <span>screen</span> or <span>tmux</span> to maintain the session).

4. Install GCC 12

After compilation is complete, execute the installation command:

sudo make install

If the installation process completes without errors, it indicates that GCC 12 has been successfully installed in the <span>/usr/local/gcc-12</span> directory.

4. Configure Environment: Prioritize Using GCC 12

At this point, there are two GCC versions in the system (the default 4.8.5 and the newly installed 12.2.0), and you need to use “soft links” or “environment variables” to make the system prioritize using GCC 12.

1. Backup the System Default GCC (Optional but Recommended)

To avoid needing to restore the default version later, first back up the old version’s soft links:

sudo mv /usr/bin/gcc /usr/bin/gcc-4.8.5
sudo mv /usr/bin/g++ /usr/bin/g++-4.8.5

2. Create Soft Links for GCC 12

Link the newly installed GCC 12 to the <span>/usr/bin</span> directory (the system’s default command search path):

sudo ln -s /usr/local/gcc-12/bin/gcc /usr/bin/gcc
sudo ln -s /usr/local/gcc-12/bin/g++ /usr/bin/g++

3. Configure Dynamic Link Library (Key! Avoid Runtime Errors)

Programs compiled with GCC 12 need to call its built-in dynamic link libraries, so the library path must be added to the system configuration:

  1. Create a library path configuration file:
sudo echo "/usr/local/gcc-12/lib64" > /etc/ld.so.conf.d/gcc-12.conf
  1. Update the dynamic link library cache:
sudo ldconfig

5. Verify Installation: Confirm GCC 12 is Active

After completing the above steps, verify the GCC version with the following commands:

1. Check GCC Version

gcc -v
g++ -v

If the output contains “<span>gcc version 12.2.0 (GCC)</span>“, it indicates that the version switch was successful.

2. Compile a Test Program (Optional)

Write a simple C++17 feature program (such as <span>std::string_view</span>) to test whether GCC 12 supports the new features:

  1. Create a test file <span>test.cpp</span>:
vim test.cpp
  1. Write the following code:
#include<iostream>
#include<string_view>

int main() {
std::string_view sv = "Hello, GCC 12!";
std::cout << sv << std::endl;
return 0;
}
  1. Compile and run with GCC 12:
g++ -std=c++17 test.cpp -o test
./test

If the output is “<span>Hello, GCC 12!</span>“, it indicates that GCC 12 has not only been successfully installed but also supports the C++17 standard normally.

6. Common Problem Solutions

  1. Compilation prompts “Out of Memory”: If the server memory is less than 2GB, the compilation process may report an error due to insufficient memory. You can temporarily expand by creating a swap partition (reference command: <span>sudo fallocate -l 2G /swapfile && sudo mkswap /swapfile && sudo swapon /swapfile</span>).

  2. Running the program prompts “Cannot find libstdc++.so.6”: The reason is that the system’s default <span>libstdc++.so.6</span> version is too low. You need to add the GCC 12 library path to the environment variable or directly replace the soft link:

    sudo ln -s /usr/local/gcc-12/lib64/libstdc++.so.6.0.30 /usr/lib64/libstdc++.so.6
    

Thus, the installation and configuration of GCC 12 on CentOS 7 is complete. If you encounter any other issues during the operation, feel free to leave a comment for discussion~

Leave a Comment