makeFile的编写
首先输入命令 make -v
,查看是否安装
$ make -v
GNU Make 4.2.1
为 x86_64-pc-linux-gnu 编译
Copyright (C) 1988-2016 Free Software Foundation, Inc.
许可证:GPLv3+:GNU 通用公共许可证第 3 版或更新版本<http://gnu.org/licenses/gpl.html>。
本软件是自由软件:您可以自由修改和重新发布它。
在法律允许的范围内没有其他保证。
若没有安装,则输入 apt-get install make
输入vi Makefile
,进行编写
$ vi Makefile
编写具体Makefile内容
# this is make file
hello.out:max.o min.o hello.c
gcc max.o min.o hello.c
max.o:max.c
gcc -c max.c
min.o:min.c
gcc -c min.c
点击i,进行编写,第一行#号表示注释
,而hello.out是需要max.o min.o以及hello.c,才能编译
多个目标文件进行链接即
hello.out:max.o min.o hello.c
而如何编译呢,需要在下一行按Tab
键,即输入
gcc max.o min.o hello.c
编译完成后,点击键盘 esc : wq 四个键,切换模式保存退出
而我们的max.o和min.o如何得到呢
就需要输入
max.o:max.c
gcc -c max.c
实际就是一个递归,hello.out是最终需要文件,是由gcc编译而来的。
退出并保存
$ ls
hello.c Makefile max.c max.h min.c min.h
$ make
gcc -c max.c
gcc -c min.c
gcc max.o min.o hello.c
$ ls
a.out hello.c Makefile max.c max.h max.o min.c min.h min.o
查看文件发现最后是a.out,而不是hello.out,这是由于这里gcc编译采用的是默认的
# this is make file
hello.out:max.o min.o hello.c
gcc max.o min.o hello.c -o hello.out
max.o:max.c
gcc -c max.c
min.o:min.c
gcc -c min.c
更改Makefile里的内容,第三行改为hello.out的编译输出
gcc max.o min.o hello.c -o hello.out
$ vi Makefile
$ make
gcc max.o min.o hello.c -o hello.out
发现只有这一行发生了改变,也就是已经编译生成过的文件不会再重新编译,从而节约了时间