Posix & GNU Make Note
🔗Compile
Source code -> object file: "*.c -> *.o"
🔗Link
Object files -> executable: "*.o -> a.out"
🔗makefile
🔗Rules
target : prerequisites
command1
command2
...
commandn
target
is a target to be generated. It can be a object file, or a executable file or a label.prerequisites
are dependecies oftarget
. It is required to generatetarget
.commandn
are commands to generatetarget
. Lines started with a '\t'
If any of prerequisites
is newer than target
, then make will call command1
to commandn
.
If any of prerequisites
is not existed, then make will call command1
to commandn
.
在默认的方式下,也就是我们只输入make命令。那么,
- make会在当前目录下找名字叫“Makefile”或“makefile”的文件。
- 如果找到,它会找文件中的第一个目标文件(target),在上面的例子中,他会找到“edit”这个文件,并把这个文件作为最终的目标文件。
- 如果edit文件不存在,或是edit所依赖的后面的 .o 文件的文件修改时间要比edit这个文件新,那么,他就会执行后面所定义的命令来生成edit这个文件。
- 如果edit所依赖的.o文件也存在,那么make会在当前文件中找目标为.o文件的依赖性,如果找到则再根据那一个规则生成.o文件。(这有点像一个堆栈的过程)
- 当然,你的C文件和H文件是存在的啦,于是make会生成 .o 文件,然后再用 .o 文件生命make的终极任务,也就是执行文件edit了。
🔗Variables
$<
: 所有的依赖目标集
$@
: 目标集
🔗Variables Used by Built-in Rules
CC
-- the c compiler to useCXX
-- the c++ compiler to useLD
-- the linker to useCFLAGS
-- compilation flag for c source filesCXXFLAGS
-- compilation flags for c++ source filesCPPFLAGS
-- flags for the c-preprocessor (typically include file paths and symbols defined on the command line), used by c and c++LDFLAGS
-- linker flagsLDLIBS
-- libraries to link