Ubuntu18.04系统用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语言是如何编译生成文件的。