[嵌入式系统01]18.04 Ubuntu环境下的基本学习及gcc和make编译操作

本文详述了在Ubuntu 18.04系统中如何更换阿里源以优化网络,以及使用C语言进行程序开发的步骤,包括编写Hello World程序、进阶的主次程序练习,并通过Makefile进行编译。此外,还展示了在Windows下使用Visual Studio 2019的类似操作。
摘要由CSDN通过智能技术生成

本文旨在完成在18.04 Ubuntu 环境下进行设置网络参数以保证系统正常联网、利用C语言编写编译并运行简单输出程序、利用Makefile方式编程某主程序等内容。

目录

                一、网络参数的设置

                二、程序编写、编译及运行

                           1.利用gcc简单编译输出hello world!

                           2.进阶练习与make编译

                总结


一、网络参数的设置

  Ubuntu系统自带的默认源是国外源,联网操作时需要访问国外服务器,速度非常慢并且很不稳定,因此考虑替换为国内的镜像源解决这个问题,可供替换的有阿里源、中科大源、清华源等,此以阿里源为例。

#阿里源
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

具体步骤:

  输入cd /etc/apt/切换到目标源文件所在目录,输入ls -l可查看到目标文件sources.list。输入vi sources.list对其进行编写,将文件原内容删除并替换为阿里源,保存退出即可。可输入ping www.baidu.com对网络进行测试,ctrl+c停止测试。

二、程序编写、编译及运行

1.利用gcc简单编译输出hello world!

 ps:  在操作之前需要安装gcc编译器,输入gcc -v查询是否安装,若已安装则会显示gcc版本信息。若未安装,输入以下命令:

sudo apt update
sudo apt install build-essential

此安装包将安装gcc,make与c++。

1.1 选择好目录进行实验,编写代码并生成相应文件,代码如下:

#include"stdio.h"
int main()
{
  printf("hello world!");
  return 0;
}

1.2 将hello.c文件转变为hello.o可执行文件

gcc hello.c -o hello.o

1.3 运行

 

2.进阶练习与make编译

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

1) 请在ubuntu系统用gcc 命令行方式编译主程序main1.c 并运行; 

2) 请在windows系统下用你熟悉的编译工具编译主程序main1.c 并运行。

3)利用makefile编译实现上述要求

2.1 ubuntu 系统 gcc编译

  • 编写主程序main1.c
    #include"stido.h"
    
    int main()
    {
    	int x=4,y=8;                    //定义x和y变量
    	printf("%.2f\n",x2x(x,y));      //输出函数处理后的值,保留两位小数
    	return 0;
    }
  • 编写子程序sub1.c
    float x2x(int x,int y)
    {
      float a;            //返回值
      a=y/x;
      return a;
    }
  • 运行结果

2.2 windows 系统

此处用vs 2019实现,代码如下

  • 编写主程序main.c
    ​#include"stido.h"
    #include"sub.h"
    int main()
    {
    	int x = 2, y = 5;
    	float b = x2x(x, y);
    	printf_s("%.2f\n",b);
    	return 0;
    }
  • 编写头文件sub.h

    #include"stdio.h"
    float x2x(int a, int b)
    {
    	float q;
    	q = (float)b / a;
    	return q;
    }
  • 运行结果

2.3 ubuntu 系统 makefile编译

  • make编写规则

make用于编写源程序之间的关系,而makefile文件即按照规定格式编写,解释说明各个源文件如何编译并链接生成可执行文件,要求定义源文件之间的依赖关系。其编写规则如下:

<target>: <prerequisites>      //target:得到的目标文件 prerequisites:生成target所需文件或目标
[Tab键]<commands>              //commands:命令或关系

建立makefile文件并编写

main2:main1.c sub1.c
       gcc main1.c sub1.c -o main2
clean:
       rm *.o
  • 输入make进行编译,./main2以运行,make clean删除生成的*.o文件

总结

  以上就是Ubuntu系统一部分的基础学习,本文仅仅简单介绍了Ubuntu一些初学内容,gcc和make编译器。期待下次的学习

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值