在Linux下时不时会遇到一个命令“make”,这个命令到底是什么,怎么感觉Linux下好多软件在源码编译安装的时候都会用到它,甚至编译内核也会用到它。除了make还有make install,make clean等,反正每次按照网上教程敲命令就好了。从来没有关心过make是干什么的?你是属于这种情况吗?如果是,赶快认真学一学下面的内容吧!
在软件开发中,make通常被视为一种软件构建工具。该工具主要经由读取一种名为“makefile”或“Makefile”的文件来实现软件的自动化建构。
Makefile
make命令执行时,需要一个 makefile 文件,以告诉make命令如何去编译程序。
一个大型的工程项目中的源文件不计其数,其按类型、功能、模块分别放在若干个目录中。makefile定义了一系列的规则来指定工程项目中哪些文件需要先编译,哪些文件需要后编译,哪些文件需要重新编译。
我以前写的程序都只有一个源文件,难怪我从来没用过这么高级的工具
写一个求圆的周长和面积的程序(v1)
一个文件一个函数
#include
#define PI 3.141592653
int main(){
double r;
printf("Please enter the radius of the circle: ");
scanf("%lf",&r);
printf("Circumference of the circle is: ");
printf("%lf\n",r*PI*2);
printf("Area of the circle is: ");
printf("%lf\n",r*r*PI);
return 0;
}
编译运行方式:
写一个求圆的周长和面积的程序(v2)
一个文件多个函数。
#include
#define PI 3.141592653
double get_circumference(double r);
double get_area(double r);
int main(){
double r;
printf("Please enter the radius of the circle ");
scanf("%lf",&r);
printf("Circumference of the circle is: ");
printf("%lf\n",get_circumference(r));
printf("Area of the circle is: ");
printf("%lf\n",get_area(r));
return 0;
}
double get_circumference(double r){
return 2*PI*r;
}
double get_area(double r){
return PI*r*r;
}
编译运行方式:
写一个求圆的周长和面积的程序(v3)
多个文件多个函数。
文件: MyPrograme.c
#include
#include "mylib.h"
int main(){
double r;
printf("Please enter the radius of the circle:\n");
scanf("%lf",&r);
printf("Circumferenceof the circle is:\n");
printf("%lf\n",get_circumference(r));
printf("Area of the circleis:\n");
printf("%lf\n",get_area(r));
return 0;
}
文件: Mylib.h
#define PI 3.141592653
double get_circumference(double r);
double get_area(double r);
文件: Mylib.c
#include "mylib.h"double get_circumference(double r){
编译运行方式:
另外一种编译方式:
上面的编译方式,当我们修改一个文件时,不需要所有的源文件都参与编译,只需要编译改动的那个文件即可,然后最终编译为可执行文件。但是如果源文件很多,那就比较麻烦了,因此需要下面的Make了。
写一个求圆的周长和面积的程序(v4)
使用Make管理项目
makefile文件内容
hello : mylib.o myprograme.o
gcc -o hello mylib.o myprograme.o
mylib.o : mylib.c mylib.h
gcc -c mylib.c
myprograme.o: myprograme.c
gcc -c myprograme.c
clean:
rm hello mylib.o myprograme.o
操作步骤
以上的makefile文件,还非常的不标准,仅仅是为了给大家一个最容易理解的示例。大家可以先查阅资料,了解更过的内容。