C++17自gcc 7开始完全支持。 Ubuntu16.04默认gcc 版本为5.4.0。 为了支持C++17,可升级至gcc 7.5.0版本。 升级方式为:
wget http://www.netgull.com/gcc/releases/gcc-7.5.0/gcc-7.5.0.tar.gz
解压:
mkdir build
tar -xf gcc-7.5.0.tar.gz -C ./build
安装编译GCC依赖项:(Dependency installation:build GCC requires GMP4.2+, MPFR 2.4.0+, and MPC 0.8.0+ and build essential)
sudo apt-get install libgmp-dev libmpfr-dev libmpc-dev libc6-dev
sudo apt-get install build-essential
若报错“cannot find crt1.o”,则需补充安装:
sudo apt install gcc-multilib
开始配置编译:(make时间长)
cd build
mkdir /usr/local/gcc-7.5.0
../gcc-7.5.0/configure --prefix=/usr/local/gcc-7.5.0
make
make install
查看gcc、g++版本:(此时仍为默认的5.4.0)
gcc -v
g++ -v
切换gcc、g++的默认版本:
sudo update-alternatives --install /usr/bin/gcc gcc /usr/local/gcc-7.5.0/bin/gcc 7
sudo update-alternatives --install /usr/bin/g++ g++ /usr/local/gcc-7.5.0/bin/g++ 7
再次查看gcc、g++的版本,此时为7.5.0:
gcc -v
g++ -v
参考资料
[1] How to install latest stable release of GCC in Linux Mint? [2] Compiling problems: cannot find crt1.o [3] VS版本、GCC版本与C++版本的对应关系 [4] How to switch between multiple GCC and G++ compiler versions on Ubuntu 20.04 LTS Focal Fossa