通过对象的微小更改构建多个可执行文件

发布于 2024-09-04 13:08:42 字数 979 浏览 3 评论 0原文

考虑以下 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 多个对象和十几个二进制文件,每次对象列表之间只有很小的变化,因此最好不必每次都列出所有对象。

Consider the following Makefile

COMP = compiler

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

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

Is there an easy way to compile multiple executables for different values of suffix? For example, the equivalent of

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

but without redefining the whole list of objects? In the real life case I am dealing with, there might be 50+ objects and a dozen binaries to build, with only small changes between the object list each time, so it would be nice not to have to list all the objects each time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鱼窥荷 2024-09-11 13:08:42

这样就可以了。如果您可以对变化施加更多限制,它可以变得更加优雅。

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 $@

This will do it. It can be made more elegant if there are more restrictions you can put on the variations.

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 $@
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文