在 RHEL/CentOS 7 安装高版本GCC

22 篇文章 0 订阅

The GNU Compiler Collection includes front ends for C, C++, Objective-C, Fortran, Ada, and Go, as well as libraries for these languages (libstdc++,…). GCC was originally written as the compiler for the GNU operating system. The GNU system was developed to be 100% free software, free in the sense that it respects the user’s freedom.
Reference: GCC official website
CentOS 7.2 自带的默认 GCC 是 GCC 4.8.5,它不支持完整的 C++11 标准,例如,不完全支持正则表达式。 为了使用正则表达式函数,至少需要安装 GCC 4.9.0。 以下安装过程适用于 CentOS 7,未在其他系统上测试。

Build and Install the Last GCC on RHEL/CentOS 7

step 1: Downloading GCC source code

可以从官方 GNU ftp 下载 GCC 源代码。 我选的安装 9.1.0 版

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

step 2 : Build and install

与其他包不同,make建议在我们构建 GCC 的 GCC 源目录之外创建另一个构建目录。
Reference: https://stackoverflow.com/questions/39854114/set-gcc-version-for-make-in-shell

tar xzf gcc-9.1.0.tar.gz
cd gcc-9.1.0
./contrib/download_prerequisites #这里需要注意是否能连接到网络,如果不能成功下载参考https://www.jianshu.com/p/87ebf6e73576
cd ..
mkdir gcc-9.1.0-build
cd gcc-9.1.0-build
../gcc-9.1.0/configure --enable-languages=c,c++ --disable-multilib
make
sudo make install

在这里插入图片描述

(如果希望在 HOME 目录中使用 GCC,请将 --prefix=$HOME/gcc-9.1.0 添加到配置命令中)

../configure --prefix=$HOME/gcc-9.1.0 --enable-languages=c,c++ --disable-multilib

–disable-multilib 配置系统的development libraries, 不添加会出现error
configure: error: I suspect your system does not have 32-bit development libraries (libc and headers). If you have them, rerun configure with --enable-multilib. If you do not have them, and want to build a 64-bit-only compiler, rerun configure with --disable-multilib.

最后一个命令将以指定 GCC 安装位置的消息结束。

----------------------------------------------------------------------
Libraries have been installed in:
   /usr/local/lib/../lib64

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the `LD_RUN_PATH' environment variable
     during linking
   - use the `-Wl,-rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to `/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------

编译过程可能需要很长时间,需要耐心等待。 最终将在 /usr/local 下安装 GCC。 也可以在配置时使用 --prefix 选项更改安装目录。 安装后需要将 GCC 的安装目录添加到您的 PATH 和 LD_LIBRARY_PATH 以使用较新的 GCC。 可能还需要重新启动ssh session。
或者编辑你的 shell 的 rcfile (~/.bashrc) 并添加,这样就不用自己每次手动导入环境变量:

export PATH=/usr/local/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/lib64:$LD_LIBRARY_PATH

step 3: Check your GCC installation

Check GCC location

whereis gcc

Check GCC compiler version

gcc --version

step 4: Test GCC

#include <iostream>
using namespace std;

int main()
{
    	cout << "Hello World!"  << endl;
	return 0;
}

Compile & run it

g++ --std=c++14 Hello.cpp -o run
./run
Hello World!

以上只是动态库调用

GCC升级后静态库没更新问题
https://blog.csdn.net/caohao0591/article/details/87511394

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值