gpt4 book ai didi

c++ - 并行调用所有子目录上的 gnumake (-j),然后才最后运行链接器规则(即重要顺序)

转载 作者:太空狗 更新时间:2023-10-29 12:09:11 26 4
gpt4 key购买 nike

我有一个 C++ 生成文件项目。它非常适合非平行建筑。对于并行构建,它的工作效率为 99%...我唯一的问题是我无法让我的最终可执行链接行最后运行(它必须是最后发生的事情)。

我有一些限制:我不想在我的链接线上有任何 PHONY 依赖项,因为这会导致它每次都重新链接。 IE。一旦构建了我的目标,当我重新构建它时不应重新链接。

这是(稍微做作的)最小示例。请不要试图挑剔它,它真的在这里只是为了显示问题,它不是真实的,但我正在显示的问题是。您应该能够运行它并看到与我相同的问题。

# Set the default goal to build.
.DEFAULT_GOAL = build

#pretend subdirs (these don't really exist but it does not matter so long as they always try to be built)
MAKE_SUB_DIRS = 1 2 3

#pretend shared objects that are created by the pretend makefile sub directories (above)
OUTPUTS = out1.so out2.so out3.so

# Top level build goal - depends on all of the subdir makes and the target.out
.PHONY: build
build: $(MAKE_SUB_DIRS) target.out
@echo build finished

# Takes 1 second to build each of these pretend sub make directories. PHONY so always runs
.PHONY: $(MAKE_SUB_DIRS)
$(MAKE_SUB_DIRS):
@if [ ! -f out$@.so ] ; then echo making $@... ; sleep 1 ; echo a > out$@.so ; fi

# The main target, pretending that it needs out1,2 and 3 to link
# Should only run when target.out does not exist
# No PHONY deps allowed here
target.out:
@echo linking $@...
@ls $(OUTPUTS) > /dev/null
@cat $(OUTPUTS) > target.out

# Clean for convinience
clean:
@rm -rf *.so target.out

现在,我真的不在乎 make 是否正常工作,我想要的是 make -j 正常工作。这是我尝试运行它:

admin@osboxes:~/sandbox$ make clean 
admin@osboxes:~/sandbox$
admin@osboxes:~/sandbox$ make -j - 1st attempt
making 1...
making 2...
linking target.out...
making 3...
ls: cannot access 'out1.so': No such file or directory
ls: cannot access 'out2.so': No such file or directory
ls: cannot access 'out3.so': No such file or directory
makefile:24: recipe for target 'target.out' failed
make: *** [target.out] Error 2
make: *** Waiting for unfinished jobs....
admin@osboxes:~/sandbox$
admin@osboxes:~/sandbox$ make -j - 2nd attempt
linking target.out...
build finished
admin@osboxes:~/sandbox$
admin@osboxes:~/sandbox$ make -j - 3rd attempt
build finished
admin@osboxes:~/sandbox$

所以我突出显示了我运行它的三个尝试。

  • 尝试 1:您可以看到构建的所有 4 个依赖项同时启动(大约)。由于每个 making x... 都需要 1 秒,而 linking 几乎是即时的,所以我们看到了我的错误。但是,所有三个“库”都已正确构建。
  • 尝试 2:库只有在它们不存在时才会被创建(这是 bash 代码——假装做 makefile 可能做的事情)。在这种情况下,它们已经创建。所以链接现在通过了,因为它只需要库存在。
  • 尝试 3:什么都没有发生,因为不需要 :)

所以你可以看到所有的步骤都在那里,这只是对它们进行排序的问题。我希望 make sub dirs 1, 2, 3 以任何顺序并行构建,然后只有在它们全部完成后我才希望 target.out 运行(即链接器)。

虽然我不想这样调用它:$(MAKE) target.out 因为在我真正的 makefile 中我有很多变量都设置...

我已经尝试查看(从其他答案).NOT_PARALLEL 并使用 dep order operator |(管道),并且我已经尝试订购大量规则以将 target.out 放在最后....但是 -j 选项只是遍历所有这些并破坏了我的顺序:( ...必须有一些简单的方法来做到这一点吗?

最佳答案

编辑:添加将变量传递给子品牌的方法示例。通过将 $(SUBDIRS) 添加到 build 的先决条件而不是在其配方中进行优化。

我不确定我是否完全理解您的组织,但处理子目录的一种解决方案如下。我假设,有点像您的示例,构建子目录 foo 在顶级目录中生成 foo.o 。我还假设您的顶级 Makefile 定义了您希望在构建子目录时传递给子品牌的变量(VAR1VAR2...)。

VAR1    := some-value
VAR2 := some-other-value
...
SUBDIRS := foo bar baz
SUBOBJS := $(patsubst %,%.o,$(SUBDIRS))

.PHONY: build clean $(SUBDIRS)

build: $(SUBDIRS)
$(MAKE) top

$(SUBDIRS):
$(MAKE) -C $@ VAR1=$(VAR1) VAR2=$(VAR2) ...

top: top.o $(SUBOBJS)
$(CXX) $(LDFLAGS) -o $@ $^ $(LDLIBS)

top.o: top.cc
$(CXX) $(CXXFLAGS) -c $< -o $@

clean:
rm -f top top.o $(SUBOBJS)
for d in $(SUBDIRS); do $(MAKE) -C $$d clean; done

这是并行安全的,并保证只有在所有子构建完成后才会发生链接。请注意,您还可以export 要传递给子 make 的变量,而不是在命令行中传递它们:

VAR1    := some-value
VAR2 := some-other-value
...
export VAR1 VAR2 ...

关于c++ - 并行调用所有子目录上的 gnumake (-j),然后才最后运行链接器规则(即重要顺序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53575593/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com