出处:https://www.cnblogs.com/yadongliang/p/6100003.html
一.安装
yum -y install gcc gcc-c++ autoconf pcre pcre-devel make automake
yum -y install wget httpd-tools vim
1.就把gcc当成c语言编译器, g++当成c++语言编译器用就是了.(知乎)
2.wget是一个从网络上自动下载文件的自由工具, 可以在用户退出系统的之后在后台继续执行, 直到下载任务完成.(百度百科)
二.测试(查看版本信息, 编译Helloworld)
1.查看gcc版本信息
gcc --version
注:我使用的是CentOS 6.9,安装后只需gcc提示找不到命令
可以先查找安装到软件包:
yum list installed | grep gcc
然后按软件包查找安装的路径:
rpm -ql gcc.x86_64
可以找到默认安装到了/usr/bin, 此目录下有gcc文件,进入到/usr/bin,执行gcc --version查询版本
yum库里的GCC版本很低,可以参考这个路径安卓8.0版本:https://blog.csdn.net/u012453838/article/details/85286810
安装 yum仓库
[root@xxx ~]# vim /etc/yum.repos.d/slc6-devtoolset-8.repo
[devtoolset-8-rh-release]
name=devtoolset 8 rh release
baseurl=https://cbs.centos.org/repos/sclo$releasever-devtoolset-8-rh-release/x86_64/os/
gpgcheck=0
enabled=1
yum 安装
[root@xxx ~]# sudo yum install devtoolset-8-gcc devtoolset-8-gcc-c++
也可修改yum命令更新安装gdb8.2.1( yum install devtoolset-8-gdb )
使用
[root@xxx ~]# sudo source /opt/rh/devtoolset-8/enable
[root@xxx ~]# gcc --version
注:yum安装完后,原来的gcc不覆盖,所以需要执行enable脚本更新环境变量。
开机自动source
可以通过加入到profile里面开机自动source
[root@xxx ~]# vim /etc/profile
跳到最后一行加入以下内容
source /opt/rh/devtoolset-8/enable
2.编写Helloworld
创建名为ctest.c文件
touch ctest.c
编辑该文件
#include <stdio.h>
int main()
{
printf("hello world!\n");
return 0;
}
编译gcc ctest.c
注:可以使用/usr/bin/gcc ctest.c
可以看到生成了a.out文件
执行a.out
./a.out
输出结果