Make 自动化依赖

文件 #

hello.c

#include <stdio.h>
#include "hello.h"

int main(void){
	printf("%s", MESSAGE);
	return 0;
}

hello.h

#define MESSAGE "hello world"

-MMD 和 -MP 编译器选项 #

-MMD 编译时自动生成依赖文件 .d,忽略系统头文件

$ gcc -MMD -c hello.c -o hello.o
$ cat hello.d
hello.o: hello.c hello.h

-MP 为依赖文件中的每个头文件生成一个伪目标(Phony)规则,防止删除头文件后 make 报规则错误

$ gcc -MMD -MP -c hello.c -o hello.o
$ cat hello.d
hello.o: hello.c hello.h
hello.h:

若每次都 make clean 后重新编译生成 .d 文件就不需要 -MP 参数

Makefile #

实现自动化依赖:

DEPS = $(OBJS:.o=.d)
-include $(DEPS)