提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
一、准备操作和环境配置
首先下载Ubuntu16.04.8
其次换源:
Ubuntu
官方的服务器在国外,为了提高软件 安装/更新速度,ubuntu
提供了 选择最佳服务器 的功能,可以帮助我们方便的找到一个速度最快的 镜像服务器!
首先备份镜像源设置文件
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
其次编辑镜像源设置文件
sudo gedit /etc/apt/sources.list
最后选择你所需的镜像源覆盖原文件(这里我选择了阿里源)
# 阿里源
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
最后下载你所需的软件
安装vim:
sudo apt-get install vim-gtk
安装gcc:
sudo apt-get install gcc
安装make:
sudo apt install make
二、测试test
首先使用vim编写hello world
按“i"进入insert模式并编写:
编写完后先使用esc退出insert模式并输入:“:wq"完成保存退出
使用gcc命令对源文件编译,并运行:
编写一个主程序文件 main1.c 和一个子程序文件 sub1.c, 要求:子程序sub1.c 包含一个算术运算函数 float x2x(int a,int b),此函数功能为对两个输入整型参数做某个运算,将结果做浮点数返回;主程序main1.c,定义并赋值两整型变量,然后调用函数 x2x,将x2x的返回结果printf出来。
首先使用vim编写main1.c(与hello world一样的vim使用方式)
vim main1.c
其次使用vim编写sub1.h
vim sub1.h
使用gcc编译到main1文件中并运行:
gcc main1.c sub1.h -o main1
对比:在Windows下使用dev-c++:
运行:
在Ubuntu中使用makefile方式编译主程序:
Makefile 是一个脚本文件,用于自动化构建大型项目。它可以帮助您定义源文件之间的依赖关系,并在需要时进行编译。通过使用 make 工具和 makefile 文件,可以轻松地编译、链接和管理整个项目。是一个简化程序员工作的工具
首先创建一个makefile文件:
最后执行命令make,并运行test1:
总结
使用gcc命令和Windows系统的IDE进行编译可以帮助您了解程序编译和运行的方式,以及多个文件之间的链接。但是,在大型工程中,使用makefile来管理编译更加高效。总而言之,相对于单纯使用gcc命令或者Windows系统的IDE,makefile 提供了更高效、灵活和可维护的方式来编译和管理多个文件的大型工程。