這裏是我的文件夾視圖:Autodependencies與GNU使
-> tree
.
├── Makefile
└── src
├── a10.c
├── a11.c
├── a12.c
├── a13.c
├── a14.c
├── a15.c
├── a16.c
├── a1.c
├── a2.c
├── a3.c
├── a4.c
├── a5.c
├── a6.c
├── a7.c
├── a8.c
├── a9.c
├── a.c
└── test.h
a1.c
到a10.c
都是空的,只是測試,只有a.c
和test.h
已代碼:
-> cat src/a.c
#include "test.h"
int main(void)
{
printf("VAR = %d\n", VAR);
printf("VAR1 = %d\n", VAR1);
return 0;
}
-> cat src/test.h
#include <stdio.h>
我有一個一個makefile功能:
-> cat Makefile
ifeq '${INPUT}' ''
STR = LATEAST
else
STR = ${INPUT}
endif
CC := gcc
LD := gcc
PROGRAM = Test
MAKE_DIR = $(PWD)
MODULES := src
SRC_DIR := $(addprefix $(MAKE_DIR)/,$(MODULES))
BUILD_DIR := $(MAKE_DIR)/output
SRC := $(foreach sdir,$(SRC_DIR),$(wildcard $(sdir)/*.c))
OBJ := $(patsubst %.c,%.o,$(SRC))
INCLUDES := $(addprefix -I,$(SRC_DIR))
INCLUDES += -I$(MAKE_DIR)/include
#test
F_VAR = 43
F_VAR1 = 40+1+2
$(warning F_VAR=$(F_VAR), F_VAR1=$(F_VAR1))
CFLAGS :=
CFLAGS = -DVAR=41+2 -DVAR1=40+1+2
vpath %.c $(SRC_DIR)
define make-goal
$1/%.o: %.c
@$(CC) $(CFLAGS) $(INCLUDES) -c $$< -o [email protected]
@$(CC) -MM $(CFLAGS) $(INCLUDES) $$< > $1/$$*.d
@echo "Compile $$*.c"
endef
.PHONY: all checkdirs clean help flowchart
all: checkdirs $(BUILD_DIR)/$(PROGRAM)
$(BUILD_DIR)/$(PROGRAM): $(OBJ)
@echo "${STR}"
@$(LD) $^ -o [email protected]
@echo "Generate $(PROGRAM)"
checkdirs: $(BUILD_DIR)
$(BUILD_DIR):
mkdir -p [email protected]
clean:
rm -f $(OBJ) $(BUILD_DIR)/$(PROGRAM)
rm -rf $(BUILD_DIR)
help:
@echo "SRC DIR: $(SRC_DIR)"
@echo "Build DIR: $(BUILD_DIR)"
@echo "Source: $(SRC)"
@echo "Obj: $(OBJ)"
@echo "Includes: $(INCLUDES)"
flowchart:
@cflow2dot pdf ${SRC}
$(foreach sdir,$(SRC_DIR),$(eval $(call make-goal,$(sdir))))
我的quesa當我碰到一個頭文件時,它沒有觸發重新編譯,我的makefile中有任何問題?
-> make
Makefile:28: F_VAR=43, F_VAR1=40+1+2
mkdir -p /home/haochen/Work/test_code/test_make/output
Compile a10.c
Compile a11.c
Compile a12.c
Compile a13.c
Compile a14.c
Compile a15.c
Compile a16.c
Compile a1.c
Compile a2.c
Compile a3.c
Compile a4.c
Compile a5.c
Compile a6.c
Compile a7.c
Compile a8.c
Compile a9.c
Compile a.c
LATEAST
Generate Test
的Autodependenciescan產生:
-> cat src/a.d
a.o: /home/haochen/Work/test_code/test_make/src/a.c \
/home/haochen/Work/test_code/test_make/src/test.h
問題: 我改性test.h,添加一個錯誤:
-> vim src/test.h
-> cat src/test.h
#include <stdio.h>
#error here
無需重新編譯:
-> make
Makefile:28: F_VAR=43, F_VAR1=40+1+2
make: Nothing to be done for `all'.
UPDATE:
我補充一下:
DEP := $(patsubst %.c,%.d,$(SRC))
同時,我在make文件的最後一行添加-include
:
-include $(DEP)
我沒有看到任何改善
至於我可以告訴你的Makefile中不使用* .d文件。 – melpomene
@melpomene,所以你的意思是我需要在生成文件中使用(編寫一些代碼)生成的* .d場。 –
如何讓應該知道如何處理* .d文件?您需要在Makefile中包含或包含* .d文件。 – Jens