multithreading 多线程make
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23814510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multi-threaded make
提问by user1088084
I can multi-thread a make with make -jN
我可以多线程一个 make make -jN
Can I dictate multi-threading withinthe Makefile so that just make
from the command-line runs multiple threads. Here's my makefile:
我可以在Makefile 中指定多线程,以便仅make
从命令行运行多个线程。这是我的生成文件:
BIN_OBJS = $(wildcard *.bin)
HEX_OBJS = $(subst .bin,.hex,$(BIN_OBJS))
all: $(HEX_OBJS)
$(HEX_OBJS): %.hex: %.bin
python ../../tools/bin2h.py $< > $@
回答by MadScientist
First, to be clear, make is not multi-threaded. Using -j
just tells make to run multiple commands at the same time (in the background, basically).
首先,需要明确的是,make 不是多线程的。使用-j
just 告诉 make 同时运行多个命令(基本上是在后台)。
Second, no, it's not possible to enable multiple jobs from within the makefile. You don't want to do that, in general, anyway because other systems will have different numbers of cores and whatever value you choose won't work well on those systems.
其次,不,不可能从 makefile 中启用多个作业。一般来说,您不想这样做,因为其他系统将具有不同数量的内核,并且您选择的任何值在这些系统上都无法正常工作。
You don't have to write multiple makefiles, though, you can just use:
不过,您不必编写多个 makefile,只需使用:
BIN_OBJS = $(wildcard *.bin)
HEX_OBJS = $(subst .bin,.hex,$(BIN_OBJS))
.PHONY: all multi
multi:
$(MAKE) -j8 all
all: $(HEX_OBJS)
$(HEX_OBJS): %.hex: %.bin
python ../../tools/bin2h.py $< > $@
回答by Alec Keeler
Be careful using -j if the filesystem where the make is occurring is an nfs share. I have seen odd results and had it mentioned to me that nfs mounted directories operate differently (some sort of file lock issue?)
如果发生 make 的文件系统是 nfs 共享,请小心使用 -j。我看到了奇怪的结果,并告诉我 nfs 挂载目录的操作方式不同(某种文件锁定问题?)
I ran my multi makes from a script and checked cpuinfo to find out how many processors the build box had (was running same script against multiple architectures/build machines)
我从脚本运行我的 multi-make 并检查 cpuinfo 以找出构建框有多少个处理器(针对多个架构/构建机器运行相同的脚本)
CPUCOUNT=$(grep -c "^processor" /proc/cpuinfo)
if [ ${CPUCOUNT} -lt 1 -o ${CPUCOUNT} -gt 4 ]
then
echo "Unexpected value for number of cpus, ${CPUCOUNT}, exiting ..."
exit 16
fi
echo "This machine has ${CPUCOUNT} cpus, will use make -j${CPUCOUNT} where possible"