Upgrade gcc to 10.4.0 for rpm-based Linux distributions

Preface

       Recently, I started to build an environment on the server due to work needs. Among them, operations that require upgrading gcc were used. After consulting a large number of blogs, I combined my own practical operations to sort them out. I hope it can help people in need. The author's operating system: kylin v10, a domestic deeply customized Linux system based on the rpm system, and the machine is an x86 virtual machine

        Since there are certain risks in upgrading gcc, you can perform a KVM snapshot before the operation to retain the current working status of the virtual machine. If a failure occurs, the environment can be restored in time to prevent damage to the original environment.

Enter in the host terminal

virsh list --all

The above command can help us view the id and name of the virtual machine

Then enter the following command to perform the snapshot operation

virsh snapshot-create-as --domain <id> --name <shotname> --description "some message" --atomic

Note: Please replace <id> in the above command with the id of the virtual machine that needs to be operated from the previous step. <shotname> is the name you gave for this snapshot operation. "some message" is the name for this snapshot operation. Some description information of snapshot operations

After executing the above command, Domain snapshot <shotname> created will be prompted, indicating that the snapshot was successfully created.

As for how to restore a snapshot:

Two methods are provided here:

1. Use the virt-manager graphical interface tool shaped like Xmanager:

1. Open the virt-manager graphical interface tool

2. Select the virtual machine for which the snapshot has been created in the virtual machine list.

3. Right-click to select the virtual machine and select the "Manage Snapshot" or "Restore Snapshot" option

4. In the pop-up window, select the snapshot to be restored and confirm the restore operation

5. Click the "Restore" button to complete the process of restoring the snapshot.

2. Use the virsh command line tool:

1. Open a terminal window and enter the command line interface

2. Use the following command to list the name and id of the virtual machine:

virsh list --all

2. If the virtual machine you want to restore the snapshot to is running, you need to shut down the virtual machine first before proceeding:

virsh destroy <domain-name>

Please replace the above <domain-name> with the name of the virtual machine you want to restore as needed.

3. Use the following command to select the virtual machine to restore that created the snapshot:

virsh snapshot-revert --domain <idorname> --snapshotname <shotname>

Modify the <idorname> and <shotname> fields in the above command according to the actual situation

1. Download the source code package

wget http://ftp.gnu.org/gnu/gcc/gcc-10.4.0/gcc-10.4.0.tar.gz

The above commands are downloaded from the GNU official website. The speed is generally slow. You can directly use domestic image sources: Alibaba Cloud, Tencent Cloud, etc. Alibaba Cloud is used here.

wget https://mirrors.aliyun.com/gnu/gcc/gcc-10.4.0/gcc-10.4.0.tar.gz

After the download is complete, unzip

tar -zxvf gcc-10.4.0.tar.gz -C /usr/local/src

Enter the unzipped source code directory

cd /usr/local/src/gcc-10.4.0/

2. gcc dependency package

In the gcc source code directory, execute the following command to check the installation:

./contrib/download_prerequisites 

The execution results are as follows

2023-07-13 10:09:28 URL:http://gcc.gnu.org/pub/gcc/infrastructure/gmp-6.1.0.tar.bz2 [2383840/2383840] -> "./gmp-6.1.0.tar.bz2" [1]
2023-07-13 10:10:44 URL:http://gcc.gnu.org/pub/gcc/infrastructure/mpfr-3.1.6.tar.bz2 [1287202/1287202] -> "./mpfr-3.1.6.tar.bz2" [1]
2023-07-13 10:11:07 URL:http://gcc.gnu.org/pub/gcc/infrastructure/mpc-1.0.3.tar.gz [669925/669925] -> "./mpc-1.0.3.tar.gz" [1]
2023-07-13 10:12:15 URL:http://gcc.gnu.org/pub/gcc/infrastructure/isl-0.18.tar.bz2 [1658291/1658291] -> "./isl-0.18.tar.bz2" [1]
gmp-6.1.0.tar.bz2: OK
mpfr-3.1.6.tar.bz2: OK
mpc-1.0.3.tar.gz: OK
isl-0.18.tar.bz2: OK
All prerequisites downloaded successfully.

This step is to download and unzip the four related dependency packages. In other blogs, most of them download them manually, and then unzip and name them separately.

3. Configuration and compilation

Create an empty build folder in the source code directory and compile within it

mkdir build
cd build

and execute the following command

../configure --prefix=/usr/local/gcc-10.4.0/ --mandir=/usr/share/man --infodir=/usr/share/info --enable-bootstrap --enable-shared \
--enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions \
--enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,lto --enable-plugin \
--enable-initfini-array --disable-libgcj --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --disable-multilib

The last one used here is --disable-multilib. If you want to compile 32-bit version, you can use --enable-multilib, but this may cause the compilation speed to be slower.

4. Compile and install

Execute the compilation command in the build directory:

make -j$(nproc)

illustrate:

1. The entire compilation process will vary greatly depending on the current machine configuration. The author compiled it myself and ran it for 1h+. Check related blogs. Some articles even take several hours.

2. "$(nproc)" is the number of cores in each physical CPU of the current server. You can fill in the value according to actual needs, such as: -j4, etc.

Here is a brief command to view the CPU information of the current machine:

lscpu

 ps: Here the author provides another command, which can also be executed directly

make -j`nproc`

After compiling, enter the following command to install

make install -j`nproc`

5. Replace and install the original version of gcc

Here you can directly uninstall the original version of gcc as needed (the uninstall command here will be slightly different depending on the specific classification of the operating system):

yum remove gcc

After completing the installation, create a soft link through the following link:

update-alternatives --install /usr/bin/gcc gcc /usr/local/gcc-10.4.0/bin/gcc 100 \
--slave /usr/bin/g++ g++ /usr/local/gcc-10.4.0/bin/g++ \
--slave /usr/bin/gcov gcov /usr/local/gcc-10.4.0/bin/gcov \
--slave /usr/bin/c++ c++ /usr/local/gcc-10.4.0/bin/c++ \
--slave /usr/bin/cc cc /usr/local/gcc-10.4.0/bin/cc \
--slave /usr/lib64/libstdc++.so.6 libstdc++.so /usr/local/gcc-10.4.0/lib64/libstdc++.so.6.0.28

Final verification of installation:

gcc --version

 If the above information is displayed, the installation is successful!

おすすめ

転載: blog.csdn.net/weixin_44110324/article/details/131700751