← 返回首页
CentOS7编写C程序
发表时间:2022-06-15 17:44:34
CentOS7编写C程序

1.检查使用已经安装gcc

[root@cvhzad8gi7gtlwbz ~]# gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)
Copyright © 2015 Free Software Foundation, Inc.
本程序是自由软件;请参看源代码的版权声明。本软件没有任何担保;
包括没有适销性和某一专用目的下的适用性担保。

如果没有安装gcc, 使用以下命令安装gcc。

[root@cvhzad8gi7gtlwbz ~]# yum install gcc

2.编写C程序

[root@cvhzad8gi7gtlwbz ~]# mkdir test
[root@cvhzad8gi7gtlwbz ~]# cd test
[root@cvhzad8gi7gtlwbz ~]# vim HelloWorld.c
#include <stdio.h>
int main(){
   printf("hello,world!\n");
   return 0;
}

3.编译执行C程序

[root@cvhzad8gi7gtlwbz ~]# gcc HelloWorld.c -o HelloWorld.out
[root@cvhzad8gi7gtlwbz ~]# ./HelloWorld.out

4.安装最新版gcc、g++

先卸载以前的旧版本:

[root@cvhzad8gi7gtlwbz ~]# rpm -q gcc

#卸载掉旧版本
[root@cvhzad8gi7gtlwbz ~]# rpm -e [上一步查到的版本号]

#验证卸载
[root@cvhzad8gi7gtlwbz ~]# gcc -v
-bash: /usr/bin/gcc: No such file or directory
[root@cvhzad8gi7gtlwbz ~]# g++ -v
-bash: /usr/bin/g++: No such file or directory

安装最新版本的gcc 、g++

#在系统中安装存储库
[root@cvhzad8gi7gtlwbz ~]# yum install centos-release-scl

#安装gcc和g++包(注意版本号:11版)
[root@cvhzad8gi7gtlwbz ~]# yum install -y devtoolset-11-gcc devtoolset-11-gcc-c++

#配置安装好的包(注意版本号:11版)
[root@cvhzad8gi7gtlwbz ~]# scl enable devtoolset-11 bash

#验证安装
[root@cvhzad8gi7gtlwbz ~]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-11/root/usr/libexec/gcc/x86_64-redhat-linux/11/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-11/root/usr --mandir=/opt/rh/devtoolset-11/root/usr/share/man --infodir=/opt/rh/devtoolset-11/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --with-default-libstdcxx-abi=gcc4-compatible --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-11.2.1-20210728/obj-x86_64-redhat-linux/isl-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 11.2.1 20210728 (Red Hat 11.2.1-1) (GCC)
[root@cvhzad8gi7gtlwbz ~]# g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-11/root/usr/libexec/gcc/x86_64-redhat-linux/11/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-11/root/usr --mandir=/opt/rh/devtoolset-11/root/usr/share/man --infodir=/opt/rh/devtoolset-11/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --with-default-libstdcxx-abi=gcc4-compatible --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-11.2.1-20210728/obj-x86_64-redhat-linux/isl-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 11.2.1 20210728 (Red Hat 11.2.1-1) (GCC) 


#配置环境变量
#先查看gcc和g++安装位置
[root@cvhzad8gi7gtlwbz ~]# which gcc
/opt/rh/devtoolset-11/root/usr/bin/gcc
[root@cvhzad8gi7gtlwbz ~]# which g++
/opt/rh/devtoolset-11/root/usr/bin/g++


#配置环境变量
[root@cvhzad8gi7gtlwbz ~]# vim /etc/profile
export PATH=$PATH:/opt/rh/devtoolset-11/root/usr/bin
[root@cvhzad8gi7gtlwbz ~]# . /etc/profile