Linux 安装 gcc 编译运行 C程序

安装gcc$ yum -y install gcc

# 安装c语言编译器gcc
[root@bogon ~]# yum -y install gcc
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.bupt.edu.cn
 * extras: mirrors.bupt.edu.cn
 * updates: mirrors.neusoft.edu.cn
base                                                                             | 3.6 kB  00:00:00     
extras                                                                           | 2.9 kB  00:00:00     
updates                                                                          | 2.9 kB  00:00:00     
updates/7/x86_64/primary_db                                                      |  13 MB  00:00:19     
正在解决依赖关系
--> 正在检查事务
---> 软件包 gcc.x86_64.0.4.8.5-44.el7 将被 安装
--> 正在处理依赖关系 cpp = 4.8.5-44.el7,它被软件包 gcc-4.8.5-44.el7.x86_64 需要
--> 正在处理依赖关系 glibc-devel >= 2.2.90-12,它被软件包 gcc-4.8.5-44.el7.x86_64 需要
--> 正在处理依赖关系 libmpfr.so.4()(64bit),它被软件包 gcc-4.8.5-44.el7.x86_64 需要
--> 正在处理依赖关系 libmpc.so.3()(64bit),它被软件包 gcc-4.8.5-44.el7.x86_64 需要
--> 正在检查事务
---> 软件包 cpp.x86_64.0.4.8.5-44.el7 将被 安装
---> 软件包 glibc-devel.x86_64.0.2.17-325.el7_9 将被 安装
--> 正在处理依赖关系 glibc-headers = 2.17-325.el7_9,它被软件包 glibc-devel-2.17-325.el7_9.x86_64 需要
--> 正在处理依赖关系 glibc = 2.17-325.el7_9,它被软件包 glibc-devel-2.17-325.el7_9.x86_64 需要
--> 正在处理依赖关系 glibc-headers,它被软件包 glibc-devel-2.17-325.el7_9.x86_64 需要
---> 软件包 libmpc.x86_64.0.1.0.1-3.el7 将被 安装
---> 软件包 mpfr.x86_64.0.3.1.1-4.el7 将被 安装
--> 正在检查事务
---> 软件包 glibc.x86_64.0.2.17-317.el7 将被 升级
--> 正在处理依赖关系 glibc = 2.17-317.el7,它被软件包 glibc-common-2.17-317.el7.x86_64 需要
---> 软件包 glibc.x86_64.0.2.17-325.el7_9 将被 更新
---> 软件包 glibc-headers.x86_64.0.2.17-325.el7_9 将被 安装
--> 正在处理依赖关系 kernel-headers >= 2.2.1,它被软件包 glibc-headers-2.17-325.el7_9.x86_64 需要
--> 正在处理依赖关系 kernel-headers,它被软件包 glibc-headers-2.17-325.el7_9.x86_64 需要
--> 正在检查事务
---> 软件包 glibc-common.x86_64.0.2.17-317.el7 将被 升级
---> 软件包 glibc-common.x86_64.0.2.17-325.el7_9 将被 更新
---> 软件包 kernel-headers.x86_64.0.3.10.0-1160.49.1.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

========================================================================================================
 Package                   架构              版本                              源                  大小
========================================================================================================
正在安装:
 gcc                       x86_64            4.8.5-44.el7                      base                16 M
为依赖而安装:
 cpp                       x86_64            4.8.5-44.el7                      base               5.9 M
 glibc-devel               x86_64            2.17-325.el7_9                    updates            1.1 M
 glibc-headers             x86_64            2.17-325.el7_9                    updates            691 k
 kernel-headers            x86_64            3.10.0-1160.49.1.el7              updates            9.0 M
 libmpc                    x86_64            1.0.1-3.el7                       base                51 k
 mpfr                      x86_64            3.1.1-4.el7                       base               203 k
为依赖而更新:
 glibc                     x86_64            2.17-325.el7_9                    updates            3.6 M
 glibc-common              x86_64            2.17-325.el7_9                    updates             12 M

事务概要
========================================================================================================
安装  1 软件包 (+6 依赖软件包)
升级           ( 2 依赖软件包)

总下载量:48 M
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
(1/9): glibc-devel-2.17-325.el7_9.x86_64.rpm                                     | 1.1 MB  00:00:01     
(2/9): glibc-headers-2.17-325.el7_9.x86_64.rpm                                   | 691 kB  00:00:00     
(3/9): kernel-headers-3.10.0-1160.49.1.el7.x86_64.rpm                            | 9.0 MB  00:00:04     
(4/9): libmpc-1.0.1-3.el7.x86_64.rpm                                             |  51 kB  00:00:00     
(5/9): mpfr-3.1.1-4.el7.x86_64.rpm                                               | 203 kB  00:00:00     
(6/9): glibc-2.17-325.el7_9.x86_64.rpm                                           | 3.6 MB  00:00:18     
(7/9): cpp-4.8.5-44.el7.x86_64.rpm                                               | 5.9 MB  00:00:23     
(8/9): glibc-common-2.17-325.el7_9.x86_64.rpm                                    |  12 MB  00:00:26     
(9/9): gcc-4.8.5-44.el7.x86_64.rpm                                               |  16 MB  00:00:28     
--------------------------------------------------------------------------------------------------------
总计                                                                    1.7 MB/s |  48 MB  00:00:28     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在更新    : glibc-2.17-325.el7_9.x86_64                                                        1/11 
  正在更新    : glibc-common-2.17-325.el7_9.x86_64                                                 2/11 
  正在安装    : mpfr-3.1.1-4.el7.x86_64                                                            3/11 
  正在安装    : libmpc-1.0.1-3.el7.x86_64                                                          4/11 
  正在安装    : cpp-4.8.5-44.el7.x86_64                                                            5/11 
  正在安装    : kernel-headers-3.10.0-1160.49.1.el7.x86_64                                         6/11 
  正在安装    : glibc-headers-2.17-325.el7_9.x86_64                                                7/11 
  正在安装    : glibc-devel-2.17-325.el7_9.x86_64                                                  8/11 
  正在安装    : gcc-4.8.5-44.el7.x86_64                                                            9/11 
  清理        : glibc-2.17-317.el7.x86_64                                                         10/11 
  清理        : glibc-common-2.17-317.el7.x86_64                                                  11/11 
  验证中      : mpfr-3.1.1-4.el7.x86_64                                                            1/11 
  验证中      : glibc-devel-2.17-325.el7_9.x86_64                                                  2/11 
  验证中      : gcc-4.8.5-44.el7.x86_64                                                            3/11 
  验证中      : glibc-headers-2.17-325.el7_9.x86_64                                                4/11 
  验证中      : kernel-headers-3.10.0-1160.49.1.el7.x86_64                                         5/11 
  验证中      : libmpc-1.0.1-3.el7.x86_64                                                          6/11 
  验证中      : glibc-common-2.17-325.el7_9.x86_64                                                 7/11 
  验证中      : glibc-2.17-325.el7_9.x86_64                                                        8/11 
  验证中      : cpp-4.8.5-44.el7.x86_64                                                            9/11 
  验证中      : glibc-2.17-317.el7.x86_64                                                         10/11 
  验证中      : glibc-common-2.17-317.el7.x86_64                                                  11/11 

已安装:
  gcc.x86_64 0:4.8.5-44.el7                                                                             

作为依赖被安装:
  cpp.x86_64 0:4.8.5-44.el7                       glibc-devel.x86_64 0:2.17-325.el7_9                   
  glibc-headers.x86_64 0:2.17-325.el7_9           kernel-headers.x86_64 0:3.10.0-1160.49.1.el7          
  libmpc.x86_64 0:1.0.1-3.el7                     mpfr.x86_64 0:3.1.1-4.el7                             

作为依赖被升级:
  glibc.x86_64 0:2.17-325.el7_9                   glibc-common.x86_64 0:2.17-325.el7_9                  

完毕!

验证安装$ gcc -v

[root@VM-0-9-centos ~]# gcc -v
使用内建 specs。
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
目标:x86_64-redhat-linux
配置为:../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
线程模型:posix
gcc 版本 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 

创建编辑源文件

执行命令 $ vi hello.c
进入 vim编辑器 , 进入默认为 [命令模式],无法输入,键入英语字符[i]或[a]或[o]键,或者insert键,进入编辑模式(下有提示为 INSERT). 开始编写文件如下:
在这里插入图片描述

  • 编辑完成后,按下[Esc]键,退出编辑模式. 进入命令模式.输入:号,进入底线命令模式 , 再输入wq ,保存退出

  • 编译文件,会产生一个 a.out的输出文件.

[root@VM-0-9-centos C_language]# vi hello.c
[root@VM-0-9-centos C_language]# gcc hello.c 
[root@VM-0-9-centos C_language]# ls
a.out  hello.c
  • 运行a.out ./ ,成功打印出 Hello World

在这里插入图片描述

当然也可以一起使用,编译加运行 $ gcc hello.c && ./a.out
在这里插入图片描述

一些其他gcc命令

  • gcc hello.c -o hello.out指定输出文件名
    在这里插入图片描述

  • 仅执行编译操作,不进行连接操作: $ gcc -c hello.c 生成.o 文件
    在这里插入图片描述

  • 执行编译预处理: $ gcc -E hello.c
    在这里插入图片描述

  • $ gcc -S .\HelloWorld.c 编译为汇编代码. 生成.s文件
    在这里插入图片描述
    查看文件内容如下.

[root@VM-0-9-centos C_language]# cat hello.s
        .file   "hello.c"
        .section        .rodata
.LC0:
        .string "Hello World"
        .text
        .globl  main
        .type   main, @function
main:
.LFB0:
        .cfi_startproc
        pushq   %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset 6, -16
        movq    %rsp, %rbp
        .cfi_def_cfa_register 6
        movl    $.LC0, %edi
        movl    $0, %eax
        call    printf
        movl    $0, %eax
        popq    %rbp
        .cfi_def_cfa 7, 8
        ret
        .cfi_endproc
.LFE0:
        .size   main, .-main
        .ident  "GCC: (GNU) 4.8.5 20150623 (Red Hat 4.8.5-44)"
        .section        .note.GNU-stack,"",@progbits

Linux下编译及运行c

安装

[root@izuf6567rc0o5t7oi23kmqz ~]# yum -y install gcc

查看版本

[root@izuf6567rc0o5t7oi23kmqz ~]# gcc -v
# gcc 版本 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
[root@izuf6567rc0o5t7oi23kmqz ~]# cd /
[root@izuf6567rc0o5t7oi23kmqz /]# ls
bin   data  etc   lib    lost+found  mnt  proc  run   srv   sys  usr
boot  dev   home  lib64  media       opt  root  sbin  swap  tmp  var
[root@izuf6567rc0o5t7oi23kmqz /]# cd data
[root@izuf6567rc0o5t7oi23kmqz data]# ll
总用量 0
[root@izuf6567rc0o5t7oi23kmqz data]# vi HelloWorld.c
[root@izuf6567rc0o5t7oi23kmqz data]# ll
总用量 4
-rw-r--r-- 1 root root 75 1229 16:00 HelloWorld.c
[root@izuf6567rc0o5t7oi23kmqz data]# gcc HelloWorld.c 
[root@izuf6567rc0o5t7oi23kmqz data]# ll
总用量 16
-rwxr-xr-x 1 root root 8560 1229 16:01 a.out
-rw-r--r-- 1 root root   75 1229 16:00 HelloWorld.c
[root@izuf6567rc0o5t7oi23kmqz data]# ./a.out 
Hello World[root@izuf6567rc0o5t7oi23kmqz data]# rm -rf a.out 

同时运行

[root@VM-0-9-centos data]# gcc hello.c && ./a.out
Hello world[root@VM-0-9-centos data]# ls
a.out  C_language  hello.c
  • 0
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦中千秋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值