Ubuntu系统下C语言的简单使用及gcc和Makefile编译C程序


本文主要讲述在Windows环境下通过虚拟机安装Ubuntu18.04,并设置网络常数,学习简单的常用的命令,Makefile编译c程序。

一、网络设置

1、 下载 Ubuntu 18.04

http://old-releases.ubuntu.com/releases/18.04.4/?_ga=2.44113060.1243545826.1617173008-2055924693.1608557140

2、下载 VMware Workstation

https://pan.baidu.com/s/1cSzZpvkbrEL6wwy6MYjP4g
提取码:pcww

3、安装虚拟机

网上教程特别多(跳过)

二、更换apt源

1、查看ubuntu的Codename


```cpp
​
lsb_release -a | grep Codename | awk '{print $2}' # 输出结果为下文中的Codename



在这里插入图片描述

2 、备份系统源

​
cd /etc/apt
sudo mv sources.list sources.list.bak

​

3、写入阿里云的源


```cpp
​
vi sources.list

​
下面源信息中$Codename为第一步中系统的Codename,用记事本批量替换即可

```cpp
​
deb http://mirrors.aliyun.com/ubuntu/ $Codename main multiverse restricted universe
deb http://mirrors.aliyun.com/ubuntu/ $Codename-backports main multiverse restricted universe
deb http://mirrors.aliyun.com/ubuntu/ $Codename-proposed main multiverse restricted universe
deb http://mirrors.aliyun.com/ubuntu/ $Codename-security main multiverse restricted universe
deb http://mirrors.aliyun.com/ubuntu/ $Codename-updates main multiverse restricted universe
deb-src http://mirrors.aliyun.com/ubuntu/ $Codename main multiverse restricted universe
deb-src http://mirrors.aliyun.com/ubuntu/ $Codename-backports main multiverse restricted universe
deb-src http://mirrors.aliyun.com/ubuntu/ $Codename-proposed main multiverse restricted universe
deb-src http://mirrors.aliyun.com/ubuntu/ $Codename-security main multiverse restricted universe
deb-src http://mirrors.aliyun.com/ubuntu/ $Codename-updates main multiverse restricted universe

​

4、执行更新

​
apt-get update

​

三、使用命令用c语言编程hello world。

1、安装使用vim、gcc。

​
audo apt-get install vim

​

在这里插入图片描述

​
sudo apt-get install gcc

​

在这里插入图片描述

2.建立和编译text.c

(1)通过vim创建一个文档text.c;

vim text.c

(2)输入相应的程序

#include<stdio,h>
#include<stdlib.h>
int main (){
printf ("hello world!\n");
return 0;
}

在这里插入图片描述

四、编写main1.c(主)和sub1.c(次)函数。

1、Ubuntu系统用gcc命令编译。

(1)建立 main1.c

vim main1.c

#include"sub1.h"
int main ()
{
int x=2,y=9;
printf("%.2f\n",x2x(x,y));
return 0;
}

(2)建立sub1.c

vim sub1.c

#ifndef __SUB1_H
#define __SUB1_H
#include<stdio.h>
float x2x (int a,int b);
#endif
#include "sub1.h"
float x2x(int a,int b);
{
float ans;
ans =(float)b/a;
return ans;
}

(3)编译main1.c

gcc main1.c -o main1

(4)运行 main1.c

./main1

2、Windows系统vs2019编译。

(1)编写main1.c

​
​
#include"sub1.h"
int main ()
{
int x=2,y=9;
printf("%.2f\n",x2x(x,y));
return 0;
}

​

​

(2)编写sub1.h

​
​
#ifndef __SUB1_H
#define __SUB1_H
#include<stdio.h>
float x2x (int a,int b);
#endif
#include "sub1.h"
float x2x(int a,int b);
{
float ans;
ans =(float)b/a;
return ans;
}

​

​

五、使用makefile编写程序

1、安装make


sudo apt-get install make

在这里插入图片描述

2、编写makefile文件并编译

​
main1 :sub1.o main .c
gcc main1.c sub1.o -o main1
sub1.0:sub1.h
gcc -c sub1.c -o sub1.o
clean:
rm*.o

​

在源文件目录下输入make 编译执行文件main1

六、总结

在本次Ubuntu系统编写实验,我使用了gcc 和Make file两种可生成文件的方式。让我初步了解了Ubuntu,更让我在编译过程中进一步了解C语言是如何编译生成文件的。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值