作用

make时自动化构建工具。通过编写Makefile,告诉make要达到什么目标,这个过程需要依赖什么文件,这个过程的shell命令是什么。

  1. You can use make with any programming language whose compiler can be run with a shell command.
  2. You can use it to describe any task where some files must be updated automatically from others whenever the others change.

简单的Makefile rules

target: prerequisites
    commands
  1. target(目标)通常是生成文件的名字,或动作的名称。如果是动作名称,又称为phony target

  2. prerequisites(前置条件)是target依赖的文件,可为空。执行make时,只有它检测到依赖文件比目标文件时间戳更新,才会执行commands。当需要分行输入时,用\转义回车符

  3. commands(命令)告诉make怎么做,它们是shell命令,都要用Tab缩进

Makefile包含什么

五个部分

  • explicit rules(显式规则)
    指明每个目标的prerequisites,可能带上commands
  • implicit rules(隐式规则)
    根据文件名自动推导出commands
  • variables(变量)
    类似于宏定义,make可根据它们替换相应的字符
  • directives(指令)
    指导make的命令,比如告诉make读取其它的Makefile、舍弃Makefile中的某些部分等
  • comments(注释)
    #号开头

Makefile的名字

make的查找顺序是: GNUmakefile -> makefile -> Makefile

  • 推荐使用Makefile即可

make的工作流程

  • 第一阶段
    读取全部的Makefile -> 初始化全部变量 -> 初始化隐式和显式规则 -> 根据全部target和prerequisites建立依赖图
  • 第二阶段
    make根据第一阶段的依赖图判断哪些target需要重建



Zhu

有问题欢迎发邮件交流