假设:
programs := apps/prog1 apps/prog2 # the actual list is quite long
sources := src/prog1.cpp src/prog2.cpp # showing only 2 files
Make文件有2个目标release
和debug
。每个目标应构建bin/
目录中的每个程序,并将目标名称附加到文件名。
例如,构建release
应该创建bin/prog1_release
和bin/prog2_release
。
如何编写静态模式规则来执行此操作?
感谢。
答案 0 :(得分:3)
这样做(在GNUMake 3.81中):
BINS := $(patsubst apps/%,bin/%,$(programs)) # bin/prog1 bin/prog2 ...
release_bins := $(addsuffix _release,$(BINS)) # bin/prog1_release ...
debug_bins := $(addsuffix _debug,$(BINS)) # bin/prog1_debug ...
$(release_bins): bin/%_release: src/%.cpp
#build the binaries according to the release rule
$(debug_bins): bin/%_debug: src/%.cpp
#build the binaries according to the debug rule
release: $(release_bins)
debug: $(debug_bins)
.PHONY: release debug
# If it turns out that one of the progs needs something else too:
bin/prog20_debug: somethingElse.cpp
(有一些方法可以使这更简洁,但代价是清晰。)