Liunx编译第一个c++文件

安装 Liunx 系统

我安装的是 CentOS-7 系统,下面是链接。直接复制到迅雷下载即可,速度还挺快的,比用浏览器下载块多了。

http://mirrors.bupt.edu.cn/centos/7.9.2009/isos/x86_64/CentOS-7-x86_64-DVD-2009.iso

 

 

 搭建 gcc 环境

这个版本默认会有一个 4.x 版本的 gcc,我个人到升级 9.3。还挺费劲的,

1
2
//查看版本<br>gcc --version 
gcc -v

安装 gcc9 取自:https://blog.csdn.net/qq_34859576/article/details/121891744

1
2
3
4
5
6
7
8
9
# 安装scl源
yum install centos-release-scl scl-utils-build -y
# 安装gcc9
yum install devtoolset-9-toolchain -y
# 启动环境
scl enable devtoolset-9 bash
gcc --version
# 永久环境
echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile 

新建文件

1
touch + filename.xxx
1
2
3
4
5
//编译的第一个c++文件<br>#include <stdio.h>
int main(){
   puts("hello world!");
   return 0;
} 

编译

1
gcc demo.cpp

 编译完成会生成 a.out 文件

运行

 

 其他;

centos 默认是不给用户 root 权限的可以通过 su 然后输入密码获得权限。密码输入的时候有保护,尽管输入就可以了,只有第一次输入的时候有黑色圆点。

[hh@localhost 00_01_gcc]$ su
密码: