Ubuntu系统下运用vim、gcc、Makefile命令简单编译运行C程序

本文主要讲述在Ubuntu系统下用vim、gcc、Makefile命令对C程序进行编译运行。本文还包括对刚下载的Ubuntu进行网络配置,apt源的更换。

一、网络配置

1.以DHCP方式配置网卡
输入sudo vi /etc/network/interfaces

将其内容改为
在这里插入图片描述

2.更换apt源

输入sudo vim /etc/apt/sources.list

将内容更换为

deb https://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse

# deb https://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
# deb-src https://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

输入sudo apt-get update

sudo apt-get upgrade

等待加载完毕即可

二、C语言编写输出hello word的程序

1,输入sudo apt-get install vim下载vim编辑器

2,输入sudo apt-get install gcc下载gcc编译器

下载完成之后实验vim编辑器编写bw.c的C程序。

输入vim bw.c进行编辑
在这里插入图片描述

保存退出以后输入

gcc bw.c -o bw

./bw

输出结果为
在这里插入图片描述

三、gcc编译C程序与Windows编译C程序

1,gcc编译

  • 编写main1.c

    #include<stdio.h>
    #include"sub1.h"
    float bb(int a,int b);
    int main()
    {
            int a,b;
            a=9;
            b=2;
            printf("a/b= %f",bb(a,b));
            return 0;
    }
    
  • 编写sub1.h

    float bb(int a,int b)
    { 
            float c=(float)a;
            float d=(float)b;
            return c/d;
    }
    

    之后输入命令运行

    gcc main1.c sub1.h -o main1

    ./main1

    运行结果为
    在这里插入图片描述

2,Windows编译

  • 编写main1.c

    #include<stdio.h>
    #include"sub1.h"
    int main()
    {
            int a,b;
            a=9;
            b=2;
            printf("a/b= %f",bb(a,b));
            return 0;
    }
    
  • 编写sub1.h

    float bb(int a,int b)
    { 
            float c=(float)a;
            float d=(float)b;
            return c/d;
    }
    

    运行结果
    在这里插入图片描述

四、Makefile方式进行编译主程序

1,安装make

sudo apt-get install make

2,make的介绍

make在当前目录下寻找“Makefile”或“makefile”文件

若找到,查找文件中的第一个目标文件.o

若目标文件不存在,根据依赖关系查找.s文件

若.s文件不存在,根据依赖关系查找.i文件

若.i文件不存在,根据依赖关系查找.c文件,此时.c文件一定存在,于是生成一个.o文件,再去执行

3,makefile文件规则

<目标>: <前置条件>
[Tab]<命令>

目标是必须存在的;前置条件和命令可以只选择一个。

例如

clean:
	rm *.o

该代码的作用便是删除所有的.o文件

4,编辑makefile文件

输入vim makefile进行编辑
在这里插入图片描述

之后输入make,运行结果如下
在这里插入图片描述

五,总结

这次作业主要是回顾了一下Linux的内容,关于vim,gcc,make等命令的学习,特别令我印象深刻的是gcc对C程序的处理步骤。还有makefile的优点,极大提高了效率。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值