Linux 并行 make:将 -j8 设置为默认选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10567890/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:17:33  来源:igfitidea点击:

Parallel make: set -j8 as the default option

linuxmakefilebuildgnu-makeparallel-builds

提问by Max Frai

I can set number of threads for the build process using -jargument. For example, I have 4 cores +4 virtual. When I write: make -j8the speed increases 4 times.

我可以使用-j参数为构建过程设置线程数。例如,我有 4 个内核 +4 个虚拟。当我写:make -j8速度增加了4倍。

Is it possible to set that value as default? (For example, in Linux Gentoo, in config file, it's possible to set this default value).

是否可以将该值设置为默认值?(例如,在 Linux Gentoo 中,在配置文件中,可以设置此默认值)。

p.s. I have Arch Linux

ps 我有 Arch Linux

采纳答案by Rafa? Rawicki

Your question is not about threads, but processes (jobs) executed by make.

您的问题不是关于线程,而是由 make 执行的进程(作业)。

The simple, way to set this, when make is used from the console is adding:

当从控制台使用 make 时,设置它的简单方法是添加:

alias make="/usr/bin/make -j 8"

to your .profilefile.

到您的.profile文件。

You can also use setenv MAKEFLAGS '-j 8', but MAKEFLAGScan ignore this parameter in some scenarios, because keeping desired number of processes requires communicating with recursive makecalls. Happily this method works with current versions of GNU Make.

您也可以使用setenv MAKEFLAGS '-j 8',但MAKEFLAGS在某些情况下可以忽略此参数,因为保持所需的进程数需要通过递归make调用进行通信。令人高兴的是,这种方法适用于当前版本的 GNU Make

回答by Matt Melton

setenv MAKEFLAGS '-j8'

Hope this helps!

希望这可以帮助!

回答by gtramontina

Here's how I've done it:

这是我如何做到的:

CORES ?= $(shell sysctl -n hw.ncpu || echo 1)

all:; @$(MAKE) _all -j$(CORES)
_all: install lint test
.PHONY: all _all
…

I've basically "aliased" my default target allto a "private" _all. The command to figure out the number of cores is OSX specific, AFAIK, so you could just improve it to be more cross platform if you will. And because of the ?=assignment, we can just override it with and env variable if/when needed.

我基本上将我的默认目标“别名”all为“private” _all。计算内核数量的命令是特定于 OSX 的 AFAIK,因此如果愿意,您可以将其改进为更加跨平台。由于?=赋值,我们可以在需要时使用 env 变量覆盖它。

EDIT:

编辑:

You can also append to your MAKEFLAGSfrom within the makefile itself, like so:

您还可以MAKEFLAGS从 makefile 本身附加到您的内容,如下所示:

CPUS ?= $(shell sysctl -n hw.ncpu || echo 1)
MAKEFLAGS += --jobs=$(CPUS)
…