Ubuntu系统下编译C程序相关流程

一、网络环境及参数设置

选择一台PC机安装虚拟机软件Vmware(版本Ubuntu 18.04),我们这里使用NetworkManager配置方式。

通过NetworkManager配置网络

1. 设置虚拟机的网络适配器为NAT模式(桥接模式)

选择 虚拟机->设置->网络适配器->桥接模式
在这里插入图片描述

2. 虚拟网络编辑器设置

选择编辑->虚拟网络编辑器->更改设置->添加网络(默认Vment0)->设置为桥接模式,自动->最后确认
在这里插入图片描述

3. 网卡与DNS配置

在命令窗口输入ifconfig -a查看网卡编号
在这里插入图片描述

输入命令sudo vi /etc/network/interfaces

按a进入编辑模式,填写情况如图所示
在这里插入图片描述

编辑完成后按esc再输入:wq!,即退出保存。

二、更换apt源

这里选用阿里源,以提高软件安装速度

1. 查看版本信息

输入命令 lsb-release -c
得到本系统的系统代号bionic

2. 编辑源列表文件

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

3. 修改文件信息

将原有内容改为如下阿里源内容

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

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

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

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

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

4. 更新软件列表及软件包

更新软件包

输入命令sudo apt-get update

输入命令sudo apt-get upgrade

出现加载进度条等待完成即可

三、编写一个简单的输出hello world的C程序

1、安装vim,gcc

输入命令sudo apt-get install vim
输入命令sudo apt-get install gcc

2、新建hello.c

进入vim编辑器,键入i,进入insert模式,编写代码
编辑完成后,esc键,:wq! 退出vim编辑

3、编译hello.c

输入命令gcc hello.c -o hello
此时会生成一个hello文件

输入 ./hello

即可完成编译

四、输入两个整型参数进行运算(分别使用Linux和Windows)

1.要求

编写一个主程序文件 main1.c 和一个子程序文件 sub1.c, 要求:子程序sub1.c 包含一个算术运算函数 float x2x(int a,int b),此函数功能为对两个输入整型参数做某个运算,将结果做浮点数返回;主程序main1.c,定义并赋值两整型变量,然后调用函数 x2x,将x2x的返回结果printf出来。

2.编写C语言代码(Windows)

1.用Dev c++编写main.c文件

#include <stdio.h>
#include "sub.h"
int main() 
{
	int x,y;
	x=2;y=3;
	float r;
	r=x2x(x,y);
	printf("%f",r);
	return 0;
}

2.编写头文件sub.h

#include <stdio.h>
float x2x(int x,int y)
{
	float r;
	r=x+y;
	return r;
}

3.编写C语言代码(Linux)

利用vim编辑器编写C语言代码

vim main.c

main.c

#include<stdio.h>
#include"sub.c"
int main(){
        int x=2,y=3;
        float i;
        i=f(x,y);
        printf("%f\n",i); 
        return 0;
}  

vim sub.c

子程序sub.c

#include<stdio.h>
float f(int x,int y){
float i;
i=x+y;
return i;
}

五、用makefile编译上述程序

1.makefile文件规则

<目标>: <前置条件>
[tab]<命令> //第二行必须tab键开始,如果空格会报错,后面是命令

目标不可省略,前置条件和命令可以二选一

目标可以是文件名,指明make命令所要构建的对象,也可以是某个操作名称。

2.安装make

输入命令sudo apt-get install make

3.用vim编辑Makefile

main: main.o sub.o
        gcc -o main main.o sub.o
main.o:main.c sub.h
        gcc -c main.c
sub.o:sub.c sub.h
        gcc -c sub.c        

保存退出后,最后在命令行输入make即可实现。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值