GNU Make:如果重命名头文件,自动先决条件无法工作
GNU Make: Automatically Prerequisites can't work if rename header files
自动预置的通用 Makefile,如下所示:
SRCS := $(wildcard *.c)
OBJS := $(SRCS:%.c=%.o)
DEPS := $(OBJS:%.o=%.d)
$(OBJS): %.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<
include $(DEPS)
$(DEPS): %.d: %.c
xxx
第一次,build ok,生成的.d文件是这样的:
config.o config.d: config.c config.h
然后我将config.h重命名为config2.h,并修改config.c:
-#include "config.h"
+#include "config2.h"
再次make,Makefile生成错误:
make[1]: *** No rule to make target 'config.h', needed by 'config.d'
因为 config.d 取决于 config.h,我该如何修改我的 Makefile 来解决这个重命名问题。
真的很简单。您的 .d
文件需要这一行:
config.h:
现在当make发现config.h
不存在时,
它会 运行 不存在的配方,并高兴地相信它创造了 config.h
。然后继续。
手册说:
If a rule has no prerequisites or recipe, and the target of the rule is a nonexistent file, then make imagines this target to have been updated whenever its rule is run.
我们如何获得这条额外的线?
在过去,您会 运行 在新创建的 .d
文件上写一行 perl。如今,对于现代 gcc 变体,只需将 -MP
添加到编译器命令行即可。
-MP This option instructs CPP to add a phony target for each dependency other than the main file, causing each to depend on nothing. These dummy rules work around errors make gives if you remove header files without updating the Makefile to match.
工作是个好人。
自动预置的通用 Makefile,如下所示:
SRCS := $(wildcard *.c)
OBJS := $(SRCS:%.c=%.o)
DEPS := $(OBJS:%.o=%.d)
$(OBJS): %.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<
include $(DEPS)
$(DEPS): %.d: %.c
xxx
第一次,build ok,生成的.d文件是这样的:
config.o config.d: config.c config.h
然后我将config.h重命名为config2.h,并修改config.c:
-#include "config.h"
+#include "config2.h"
再次make,Makefile生成错误:
make[1]: *** No rule to make target 'config.h', needed by 'config.d'
因为 config.d 取决于 config.h,我该如何修改我的 Makefile 来解决这个重命名问题。
真的很简单。您的 .d
文件需要这一行:
config.h:
现在当make发现config.h
不存在时,
它会 运行 不存在的配方,并高兴地相信它创造了 config.h
。然后继续。
手册说:
If a rule has no prerequisites or recipe, and the target of the rule is a nonexistent file, then make imagines this target to have been updated whenever its rule is run.
我们如何获得这条额外的线?
在过去,您会 运行 在新创建的 .d
文件上写一行 perl。如今,对于现代 gcc 变体,只需将 -MP
添加到编译器命令行即可。
-MP This option instructs CPP to add a phony target for each dependency other than the main file, causing each to depend on nothing. These dummy rules work around errors make gives if you remove header files without updating the Makefile to match.
工作是个好人。