make的简单使用

注意:如果你想用这篇文章,搞定如何写一个简单的make,但没有知识基础请速退,点这里,下pdf,里面有详细的讲解,半个小时就足够你写一个了

maketree:maketree.o stackstdio.o treestdio.o
gcc -g maketree.o stackstdio.o treestdio.o -o maketree
maketree.o:maketree.c tree.h stackarray.h
gcc -c maketree.c
stackstdio.o:stackstdio.c stackarray.h
gcc -c stackstdio.c
treestdio.o:treestdio.c tree.h stackarray.h
gcc -c treestdio.c

#This is a makefile ,my first makefile

  

基本单元为规则
规则:制定一个或多个目标文件
跟随编译生成该目标文件所依赖的文件或模块
生成或更新目标文件所使用的命令

格式:
目标文件列表 分隔符 依赖文件列表[;命令]
[命令]
[命令]

注意: 命令是以开头的Tab键标示的的一行,没有的话,为上个规则的命令
#为注释标示
为分行输入

make 的简单运行模式
首先寻找到makefile或Makefile(可以指定 make -f othername )
打开依照规则
1.目标文件不存在,依赖文件不存在,执行下一规则
2.目标文件不存在.依赖文件存在.生成目标文件
3.目标文件存在,依赖文件存在,比较生成时间
if( 目标文件的时间 < 依赖文件 ) 重新生成
else 跳过
4.需要回溯第一条命令 ( why,类似递归的性质,但是什么决定了在开头结束
解释: 最终目标规则,把最终要生成的目标文件放在第一个规则的第一个目标文件
)

隐藏规则
例子 : module.o :headl.h
make会自动扩展为:
module.o :modele.c headl.h
gcc -c modele.c -o modele.o



原文地址:https://www.cnblogs.com/dilidingzhi/p/4066926.html