GNU make:在不同的目录中构建所有源

时间:2011-04-25 12:52:08

标签: makefile

假设:

programs := apps/prog1 apps/prog2        # the actual list is quite long
sources := src/prog1.cpp src/prog2.cpp   # showing only 2 files

Make文件有2个目标releasedebug。每个目标应构建bin/目录中的每个程序,并将目标名称附加到文件名。

例如,构建release应该创建bin/prog1_releasebin/prog2_release

如何编写静态模式规则来执行此操作?

感谢。

1 个答案:

答案 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

(有一些方法可以使这更简洁,但代价是清晰。)