Makefile通用规则从多个文件夹构建src文件

时间:2017-12-04 16:45:04

标签: makefile gnu-make

我打算保留我的所有* .o文件build / obj并拥有源文件src / src.c log / log.c

OBJECTS=build/log.o build/src.o

$(OBJ_DIR)/%.o : src/%.c log/%.c
       $(CC) $(CFLAGS) $(DEFINE) $(INCLUDE) -o$@ $<

但它没有将* .o映射到正确的* .c我做错了什么?

1 个答案:

答案 0 :(得分:0)

您可以按如下方式使用VPATH

~/tmp8> more Makefile

OBJECTS=build/log.o build/src.o
OBJ_DIR=build

all: $(OBJECTS)

VPATH=src:log

$(OBJ_DIR)/%.o: %.c
        @echo compiling "$< ==> $@"
~/tmp8> find . -type f
./src/src.c
./Makefile
./log/log.c    

~/tmp8> make
compiling log/log.c ==> build/log.o
compiling src/src.c ==> build/src.o

~/tmp8>