赞
踩
linux系统查看gcc/g++版本的命令
- gcc --version //版本for example: gcc (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609
- g++ --version //版本for example: g++ (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609
实际开发中使用的软件为了保证稳定,可能是使用较低版本的gcc/g++编译器生成的,若用户使用高版本gcc/g++编译可能造成软件报错,无法使用。例如,我系统默认安装的是gcc/g++ 5.4.0版本, 程序中调用了protobuf的库, 编译protobuf-3.3.0版本后,调用程序报错“symbol lookup error: undefined symbol: _ZN6google8protobuf8iternal26fixed_address_empty_stringE” ;解决方案:将gcc/g++降级到4.8版本,然后编译protobuf-3.3.0库,则程序正常运行。
这就涉及到了将系统gcc/g++降级。当然,有时会涉及将gcc/g++版本升级,方法类似,本文仅以gcc/g++降级为例讲解方法。
查看gcc/g++安装位置,命令:
which gcc
结果显示/usr/bin/gcc, 查看/usr/bin文件夹可以发现gcc和g++实际都是软链接(使用ls -l查看文件可以看到gcc -> gcc-5, g++ -> -> gcc-5),实际安装程序为gcc-5/g++-5, 软连接将gcc/g++分别链接到gcc-5/g++-5,所以实际使用gcc/g++编译时会链接到gcc-5/g++-5。
(使用ls -l查看文件可以看到gcc -> gcc-5, g++ -> -> gcc-5)改变gcc/g++版本只需要安装新版本,然后将gcc/g++分别链接到新版本的程序即可(linux系统这样设计程序便于升级/降级,多个版本程序可同时存在,只需要调用软链接的接口选择使用哪个版本)。
以4.8为例:
sudo apt-get install -y gcc-4.8
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
cpp-4.8 gcc-4.8-base libasan0 libcloog-isl4 libgcc-4.8-dev
Suggested packages:
gcc-4.8-locales gcc-4.8-multilib gcc-4.8-doc libgcc1-dbg libgomp1-dbg
libitm1-dbg libatomic1-dbg libasan0-dbg libtsan0-dbg libquadmath0-dbg
The following NEW packages will be installed:
cpp-4.8 gcc-4.8 gcc-4.8-base libasan0 libcloog-isl4 libgcc-4.8-dev
0 upgraded, 6 newly installed, 0 to remove and 569 not upgraded.
Need to get 11.5 MB of archives.
After this operation, 36.5 MB of additional disk space will be used.
Do you want to continue? [Y/n]
选择Y,
Get:1 http://cn.archive.ubuntu.com/ubuntu xenial/universe amd64 gcc-4.8-base amd64 4.8.5-4ubuntu2 [15.1 kB]
Get:2 http://cn.archive.ubuntu.com/ubuntu xenial/universe amd64 libcloog-isl4 amd64 0.18.4-1 [60.5 kB]
Get:3 http://cn.archive.ubuntu.com/ubuntu xenial/universe amd64 cpp-4.8 amd64 4.8.5-4ubuntu2 [4,599 kB]
Get:4 http://cn.archive.ubuntu.com/ubuntu xenial/universe amd64 libasan0 amd64 4.8.5-4ubuntu2 [63.2 kB]
Get:5 http://cn.archive.ubuntu.com/ubuntu xenial/universe amd64 libgcc-4.8-dev amd64 4.8.5-4ubuntu2 [1,687 kB]
Get:6 http://cn.archive.ubuntu.com/ubuntu xenial/universe amd64 gcc-4.8 amd64 4.8.5-4ubuntu2 [5,064 kB]
Get:6 http://cn.archive.ubuntu.com/ubuntu xenial/universe amd64 gcc-4.8 amd64 4.8.5-4ubuntu2 [5,064 kB]
Fetched 9,719 kB in 4min 36s (35.2 kB/s)
Selecting previously unselected package gcc-4.8-base:amd64.
(Reading database ... 251339 files and directories currently installed.)
Preparing to unpack .../gcc-4.8-base_4.8.5-4ubuntu2_amd64.deb ...
Unpacking gcc-4.8-base:amd64 (4.8.5-4ubuntu2) ...
Selecting previously unselected package libcloog-isl4:amd64.
Preparing to unpack .../libcloog-isl4_0.18.4-1_amd64.deb ...
Unpacking libcloog-isl4:amd64 (0.18.4-1) ...
Selecting previously unselected package cpp-4.8.
Preparing to unpack .../cpp-4.8_4.8.5-4ubuntu2_amd64.deb ...
Unpacking cpp-4.8 (4.8.5-4ubuntu2) ...
Selecting previously unselected package libasan0:amd64.
Preparing to unpack .../libasan0_4.8.5-4ubuntu2_amd64.deb ...
Unpacking libasan0:amd64 (4.8.5-4ubuntu2) ...
Selecting previously unselected package libgcc-4.8-dev:amd64.
Preparing to unpack .../libgcc-4.8-dev_4.8.5-4ubuntu2_amd64.deb ...
Unpacking libgcc-4.8-dev:amd64 (4.8.5-4ubuntu2) ...
Selecting previously unselected package gcc-4.8.
Preparing to unpack .../gcc-4.8_4.8.5-4ubuntu2_amd64.deb ...
Unpacking gcc-4.8 (4.8.5-4ubuntu2) ...
Processing triggers for libc-bin (2.23-0ubuntu11) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up gcc-4.8-base:amd64 (4.8.5-4ubuntu2) ...
Setting up libcloog-isl4:amd64 (0.18.4-1) ...
Setting up cpp-4.8 (4.8.5-4ubuntu2) ...
Setting up libasan0:amd64 (4.8.5-4ubuntu2) ...
Setting up libgcc-4.8-dev:amd64 (4.8.5-4ubuntu2) ...
Setting up gcc-4.8 (4.8.5-4ubuntu2) ...
Processing triggers for libc-bin (2.23-0ubuntu11) ...
sudo apt-get install -y g++-4.8
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
libstdc++-4.8-dev
Suggested packages:
g++-4.8-multilib gcc-4.8-doc libstdc++6-4.8-dbg libstdc++-4.8-doc
The following NEW packages will be installed:
g++-4.8 libstdc++-4.8-dev
0 upgraded, 2 newly installed, 0 to remove and 569 not upgraded.
Need to get 19.2 MB of archives.
After this operation, 40.2 MB of additional disk space will be used.
Do you want to continue?
选择Y,
Get:1 http://cn.archive.ubuntu.com/ubuntu xenial/universe amd64 libstdc++-4.8-dev amd64 4.8.5-4ubuntu2 [1,049 kB]
Get:2 http://cn.archive.ubuntu.com/ubuntu xenial/universe amd64 g++-4.8 amd64 4.8.5-4ubuntu2 [18.2 MB]
Fetched 19.2 MB in 1min 35s (202 kB/s)
Selecting previously unselected package libstdc++-4.8-dev:amd64.
(Reading database ... 251491 files and directories currently installed.)
Preparing to unpack .../libstdc++-4.8-dev_4.8.5-4ubuntu2_amd64.deb ...
Unpacking libstdc++-4.8-dev:amd64 (4.8.5-4ubuntu2) ...
Selecting previously unselected package g++-4.8.
Preparing to unpack .../g++-4.8_4.8.5-4ubuntu2_amd64.deb ...
Unpacking g++-4.8 (4.8.5-4ubuntu2) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up libstdc++-4.8-dev:amd64 (4.8.5-4ubuntu2) ...
Setting up g++-4.8 (4.8.5-4ubuntu2) ...
- cd /usr/bin
- sudo mv gcc gcc_link5_bak
- sudo mv g++ g++_link5_bak
- sudo ln -s gcc-4.8 gcc
- sudo ln -s g++-4.8 g++
gcc --version命令后显示:gcc (Ubuntu 4.8.5-4ubuntu2) 4.8.5 说明版本更换成功
注意:g++版本中存在一些bug,例如,强制要求sort中的输入参数是const,在GCC 4.9.0+版本之后修复 https://stackoverflow.com/questions/52673134/why-do-i-get-a-compilation-error-for-this-program-in-g-4-8-5
更全面的gcc/g++相关程序跟随调整可参见:https://www.jianshu.com/p/7c73e386f0e4
参考:https://www.jianshu.com/p/8d73cb869957
cmakelist中会调用默认的g++/gcc,也就是上面的软连接。
如果不想每次修改软连接,可以在cmakelist里手动指定编译器版本:
SET(CMAKE_C_COMPILER "/usr/bin/gcc-4.8")
SET(CMAKE_CXX_COMPILER "/usr/bin/g++-4.8")
更多cmake和编译内容参见:https://blog.csdn.net/Cxiazaiyu/article/details/90769079
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。