使用对象中的少量更改构建多个可执行文件

时间:2010-06-09 16:40:48

标签: makefile

考虑以下Makefile

COMP = compiler

OBJECTS =   file1 \
        file2 \
        file3 \
        file4 \
        file5_suffix \
        file6 \
        file7 \
        file8 \
        file9_suffix \
        file10

all:    $(OBJECTS) 
        $(COMP) $(OBJECTS) -o bin/executable_suffix

是否有一种简单的方法可以为suffix的不同值编译多个可执行文件?例如,相当于

COMP = compiler

OBJECTS1 =  file1 \
        file2 \
        file3 \
        file4 \
        file5_s1 \
        file6 \
        file7 \
        file8 \
        file9_s1 \
        file10

OBJECTS2 =  file1 \
        file2 \
        file3 \
        file4 \
        file5_s2 \
        file6 \
        file7 \
        file8 \
        file9_s2 \
        file10

all:    $(OBJECTS1) $(OBJECTS2) 
        $(COMP) $(OBJECTS1) -o bin/executable_s1
        $(COMP) $(OBJECTS2) -o bin/executable_s2

但没有重新定义整个对象列表?在我正在处理的现实生活中,可能会有50多个对象和十二个二进制文件构建,每次对象列表之间只有很小的变化,所以不必每次都列出所有对象。

1 个答案:

答案 0 :(得分:0)

这样做。如果您可以对变体进行更多限制,则可以使其更加优雅。

COMP = compiler

OBJECTS =  file1 \
    file2 \
    file3 \
    file4 \
    file5 \
    file6 \
    file7 \
    file8 \
    file9 \
    file10

# This isn't terribly elegant, but it is quite general.
# If the variations you have in mind are more regular, such as
# changing only file5 and file9 for all executables, then there
# are tidier ways to do it.
OBJECTS1 := $(OBJECTS)
OBJECTS1 := $(patsubst file5,file5_s1,$(OBJECTS1))
OBJECTS1 := $(patsubst file9,file9_s1,$(OBJECTS1))

OBJECTS2 := $(OBJECTS)
OBJECTS2 := $(patsubst file5,file5_s2,$(OBJECTS2))
OBJECTS2 := $(patsubst file9,file9_s2,$(OBJECTS2))

all: bin/executable_s1 bin/executable_s2

# It looks a little clumsy to include the path in the target,
# but the idea is that the target should be what it's actually making,
# which is bin/executable_s1, not executable_s1.

bin/executable_s1: $(OBJECTS1)
bin/executable_s2: $(OBJECTS2)

bin/executable_%:
    $(COMP) $^ -o $@