头文件更改时,GNU-Make无法重新编译 [英] GNU-Make does not recompile when a header file changed
问题描述
更改hdr.h文件时,GNU-Make无法重新编译. 如下面的打印行所示,即使生成了main.d文件,它也没有尝试重新编译. 你能指导我为什么会发生吗?
GNU-Make does not recompile when hdr.h file changed. As below printed lines, it did not try to recompile even main.d file is generated. Can you guide me why it happend?
hdr.h
#ifndef __HDR_H__
#define LOOP_CNT 1000
#endif /* __HDR_H__ */
main.c
#include <stdio.h>
#include "hdr.h"
int main(void)
{
int i, sum = 0;
for (i = 0; i < LOOP_CNT; i++) sum += i;
(void)printf("sum = %d\n", sum);
return 0;
}
Makefile
SUFFIXES += .d
.PHONY: clean
OBJECTS = $(patsubst %.c,%.o,$(wildcard *.c))
CC = armcc
LD = armcc
CFLAGS +=
# Default target
all: sum
sum : $(OBJECTS)
$(CC) $(CFLAGS) -o $@ $^
$(OBJECTS) : %.o : %.c
$(CC) $(CFLAGS) -o $@ -c $<
# Generating dependency files
%.d : %.c
@$(CC) -M $< > $@
# Include dependency file to have gcc recompile necessary sources
include $(patsubst %.c,%.d,$(wildcard *.c))
#$(info $(patsubst %.c,%.d,$(wildcard *.c)))
clean:
rm -f *.o *.d core $(EXEC_NAME)
此处打印的行数是秒.
C:\project\dep>make all
Makefile:24: main.d: No such file or directory
armcc -o main.o -c main.c
armcc -o sum main.o
C:\project\dep>make all
make: Nothing to be done for `all'.
main.d文件生成如下.
main.d file is generated as below.
__image.axf: main.c
__image.axf: C:\Program Files\ARM\RVCT\Data\4.1\713\include\windows\stdio.h
__image.axf: hdr.h
推荐答案
作为快速,肮脏的Makefile修复程序,如果头文件发生更改,我只列出我的所有头文件,然后将$(HEADERS)
作为依赖项添加到要构建的部分中C src文件中的目标文件.它的效率不尽如人意,但我发现它已经足够好了,即
As a quick and dirty Makefile fix for rebuilding if headers change I just list all my header files and then add $(HEADERS)
as a dependency in the part that builds the object files from the C src files. Its not as efficient as it could be but I find it to be good enough, i.e.
HEADERS = \
my_header.h \
my_other_header.h
$(BUILD_DIR)/%.o: %.c $(HEADERS)
$(LINK.c) $< -c -o $@
这篇关于头文件更改时,GNU-Make无法重新编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!