我一直在设计一个多配置Makefile(一个支持单独的'debug'和'release'目标),并且遇到了一个奇怪的问题,这似乎是GNU make中的一个bug.
当隐式规则中引用这些变量时,GNU make似乎没有正确扩展特定于目标的变量.这是一个简化的Makefile,它显示了这个问题:
all:
@echo specify configuration 'debug' or 'release'
OBJS := foo.o bar.o
BUILDDIR = .build/$(CONFIG)
TARGET = $(addprefix $(BUILDDIR)/,$(OBJS))
debug: CONFIG := debug
release: CONFIG := release
#CONFIG := debug
debug: $(TARGET)
release: $(TARGET)
clean:
rm -rf .build
$(BUILDDIR)/%.o: %.c
@echo [$(BUILDDIR)/$*.o] should be [$@]
@mkdir -p $(dir $@)
$(CC) -c $< -o $@
Run Code Online (Sandbox Code Playgroud)
指定目标'debug'时,CONFIG设置为'debug',BUILDDIR和TARGET同样正确扩展.但是,在从对象构建源文件的隐式规则中,$ @被展开,就像CONFIG不存在一样.
以下是使用此Makefile的输出:
$ make debug
[.build/debug/foo.o] should be [.build//foo.o]
cc -c foo.c -o .build//foo.o
[.build/debug/bar.o] should be [.build//bar.o]
cc -c bar.c -o …Run Code Online (Sandbox Code Playgroud)