我构建了这样一个目录树,顶层Makefile调用world/Makefile和main/Makefile.
我尝试修改world/headers/world.h的内容,在顶层make的时候没有重新编译world.c。
我疑惑的是单个Makefile自动生成依赖项是有效的,但是如果嵌套makefile就无效了。
顶层Makefile内容:
export EXE_OUTPUT_DIR = $(shell pwd)/build
export LIB_OUTPUT_DIR = $(EXE_OUTPUT_DIR)/lib
modules = world main
all:
@for subdir in $(modules); \
do \
$(MAKE) -C "$$subdir"; \
done
clean:
@for subdir in $(modules); \
do $(MAKE) -C "$$subdir" $@; \
done
.PHONY:clean
world/Makefile:
# VPATH = src
SRC_FILE = $(wildcard src/*.c)
SRC_OBJ = $(SRC_FILE:.c=.o)
TARGET = libworld.a
CFLAGS += -g -Wall -Werror
CPPFLAGS += -I. -I./headers
$(TARGET): $(SRC_OBJ)
$(AR) rcs $@ $^
cp $@ $(LIB_OUTPUT_DIR)
%.d:%.c
@$(CC) -MM $(CPPFLAGS) $< > $@.$$$$; \
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
rm -f $@.$$$$
-include $(SRC_OBJ:.o=.d)
clean:
$(RM) $(SRC_OBJ) $(TARGET) $(LIB_OUTPUT_DIR)/$(TARGET)
$(RM) $(SRC_OBJ:.o=.d)
main/Makefile:
SRC_FILE = $(wildcard *.c)
SRC_OBJ = $(SRC_FILE:.c=.o)
TARGET=target
CFLAGS += -g -Wall -Werror
CPPFLAGS += -I. -I../world/headers
LDFLAGS += -lpthread -L$(LIB_OUTPUT_DIR) -lworld
$(TARGET): $(SRC_OBJ)
$(CC) -o $@ $^ $(LDFLAGS)
cp $@ $(EXE_OUTPUT_DIR)
clean:
$(RM) $(SRC_OBJ) $(TARGET) $(EXE_OUTPUT_DIR)/$(TARGET)