当头文件更改时 GNU-Make 不会重新编译

and*_*lla 6 header gnu-make recompile armcc

当 hdr.h 文件更改时,GNU-Make 不会重新编译。如下打印行,即使生成了 main.d 文件,它也没有尝试重新编译。你能指导我为什么会这样吗?

hdr.h

#ifndef __HDR_H__  

#define LOOP_CNT 1000  

#endif /* __HDR_H__ */  
Run Code Online (Sandbox Code Playgroud)

主文件

#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;  
}  
Run Code Online (Sandbox Code Playgroud)

生成文件

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)  
Run Code Online (Sandbox Code Playgroud)

这是第二行的打印行。

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'. 
Run Code Online (Sandbox Code Playgroud)

main.d 文件生成如下。

__image.axf: main.c
__image.axf: C:\Program Files\ARM\RVCT\Data\4.1\713\include\windows\stdio.h
__image.axf: hdr.h
Run Code Online (Sandbox Code Playgroud)

bph*_*bph 3

作为一个快速而肮脏的 Makefile 修复,用于在标头更改时进行重建,我只需列出所有标头文件,然后将其$(HEADERS)作为依赖项添加到从 C src 文件构建目标文件的部分中。它的效率并不高,但我发现它已经足够好了,即

HEADERS = \
    my_header.h \
    my_other_header.h

$(BUILD_DIR)/%.o: %.c $(HEADERS)
    $(LINK.c) $< -c -o $@
Run Code Online (Sandbox Code Playgroud)