g++11 compilation and installation + virtual machine expansion

GCC installation [automatic version]

gcc official website: https://gcc.gnu.org/
gcc11 installation resources: gcc11 automatic installation script and source code
Because of the official website mirroring, the download speed is slow. I made a sorting out. I wrote a script for the convenience of installation, which can be packaged and downloaded together. Mindless installation.
Notes on using scripts:

  1. The remaining hard storage is healthy. It is recommended to keep at least 10G of free space daily. If the space is not enough, refer to the expansion introduction below.
  2. The memory size is healthy. In order to speed up the compilation, make uses 8 threads to compile. The memory may be exhausted. More than 8G is recommended. It is recommended to expand if it is less than 8G.
  3. Permission is required.

Instructions:

sudo su
sh installGcc11.sh

Screenplay:

#!/bin/sh
tar -xvf gcc-11.1.0.tar.xz
cd gcc-11.1.0
sed -i '35d' tt
sed -i "35a base_url='http://mirror.linux-ia64.org/gnu/gcc/infrastructure/' " tt
cd .. #不建议在源代码里进行编译和安装,所以退回上级文件夹
mkdir gcc11_build
cd gcc11_build
unset LIBRARY_PATH #重置环境变量
../gcc-11.1.0/configure --disable-multilib
make -j 8 #8是多线程数,可以根据电脑配置修改,如果是虚拟机建议不要开启多线程编译,本行请换成make。如果还是编不过,考虑增大swap空间,或增大虚拟机内存分配。编译时最好不要开启其他程序。
sudo make install #make install 的时候需要带上sudo是因为默认安装地址是/usr/local,需要权限。除非在make的时候选定不需要权限安装地址(不推荐)
sudo update-alternatives --install /usr/bin/gcc gcc /usr/local/bin/gcc 50 #使用update-alternatives命令配置增加最新版本编译器
sudo update-alternatives --install /usr/bin/g++ g++ /usr/local/bin/g++ 50
#下面是增加libstdc++链接
cd /usr/lib/x86_64-linux-gnu/
sudo cp libstdc++.so.6 libstdc++.so.6.backup
sudo rm libstdc++.so.6
sudo cp /usr/local/lib64/libstdc++.so.6.0.29 libstdc++.so.6.0.29
sudo ln -s libstdc++.so.6.0.29 libstdc++.so.6

GCC version switching

ls /usr/bin/gcc* #此步骤显示的内容是下一步配置的内容
ls /usr/bin/g++*
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 60 #使用update-alternatives命令配置原始编译器
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-5 60 #这两步相当于向系统编译器优先级注册,注册2个版本或以上数量,才能进行版本切换
sudo update-alternatives --config gcc #选择版本,进行切换
sudo update-alternatives --config g++
gcc -v #查看版本
g++ -v

At the same time, version switching also needs to switch libstdc++. Currently, mine is in an incompatible state, so the switching method is

switch high version

sudo update-alternatives --config gcc
sudo update-alternatives --config g++
cd /usr/lib/x86_64-linux-gnu/
sudo rm libstdc++.so.6
sudo ln -s libstdc++.so.6.0.29 libstdc++.so.6

switch low version

sudo update-alternatives --config gcc
sudo update-alternatives --config g++
cd /usr/lib/x86_64-linux-gnu/
sudo rm libstdc++.so.6
sudo ln -s libstdc++.so.6.backup libstdc++.so.6

Content reference: https://blog.csdn.net/davidhopper/article/details/79681695

GCC installation [manual version]

Official website: https://gcc.gnu.org/
Enter the official website and select the mirrors downloaded on the right. There is no ubuntu version in binary. You need to download the source code from the mirror to compile it. Enter the mirror and select the mirror of Russia. Then select the release folder in the mirror station, and select the version you need to download.
11.1 is selected here. Link: gcc-11.1.0

tar -xvf gcc-11.1.0.tar.xz 或者 tar -xzvf gcc-11.1.0.tar.gz
#前者是xz压缩方式后者是gzip压缩方式
cd gcc-11.1.0

Open the dependency package download script file: contrib/download_prerequisites
replace base_url='ftp://gcc.gnu.org/pub/gcc/infrastructure/' in the file with: base_url='http://mirror.linux-ia64 .org/gnu/gcc/infrastructure/', that is, replace the non-existent server address with the mirror server address. Next, execute the following commands to automatically download and decompress the dependent packages:

bash contrib/download_prerequisites

If this step fails, you need to continue to change the address of the mirror server, or build a ladder (dog head), and then enter the gcc source folder to delete gmp-6.1.0.tar.bz2, mpfr-3.1.4.tar.bz2, mpc- 1.0.3.tar.gz, isl-0.16.1.tar.bz2 four files, and then repeat the bash step until "All prerequisites downloaded successfully" appears, indicating success.

cd ..
mkdir gcc11_build
cd gcc11_build
unset LIBRARY_PATH #重置环境变量
../gcc-11.1.0/configure --disable-multilib
make -j 8
sudo make install

In other words, manual make and makeinstall do not have a clear indication of successful installation. Unless there is an obvious error such as error or failed, please continue to the next step. If the next step fails, return to the previous step to find the reason.

#update-alternatives --install <链接> <名称> <路径> <优先级>
sudo update-alternatives --install /usr/bin/gcc gcc /usr/local/bin/gcc 50
sudo update-alternatives --install /usr/bin/g++ g++ /usr/local/bin/g++ 50
# 查询本机已有GCC编译器情况
sudo update-alternatives --query gcc
# 查询本机已有G++编译器情况
sudo update-alternatives --query g++

Virtual machine expansion

  1. If it is a dual system, just ensure that the hardware space and hardware memory are large enough, otherwise you need to upgrade the machine or try to change make -j 8 to make.

  2. If it is a virtual machine, there are two concerns, hard storage and memory

  3. Hard storage expansion:
    There are mainly two parts: a. Virtual configuration b. Install gparted, and use it to partition
    bash sudo apt-get install gparted
    bash sudo gparted
    and then follow the steps to expand the capacity.
    Content reference: https://blog.csdn.net/daemon_2017/article/details/80660372

  4. Memory expansion:

     a.增大交换空间(swap)【不推荐】
     b.增加内存【有钱的话,非常推荐,再增大虚拟内存,就可以有更好的体验】
     c.增加虚拟内存(在虚拟机上设置一下就行)【推荐】
    

Guess you like

Origin blog.csdn.net/sinat_21699465/article/details/116601935