I have a Makefile on a machine that has a ton of cores in it, but I always seem to forget to write -jX
when compiling my project and it takes way longer than it should.
Is there some way I can set the -j
flag through an environment variable or some other persistent config file so that make will automatically execute multiple jobs in parallel on this machine?
I would just put
in
~/.profile
or~/.bashrc
.According to the manual:
As Jeremiah Willcock said, use
MAKEFLAGS
, but here is how to do it:or you could just set a fixed value like this:
If you really want to boost performance you should use
ccache
by adding something like the following to yourMakefile
:You can add a line to your Makefile similar to the following:
Then add a
${NUMJOBS}
line in your rules, or add it into anotherMakefile
var (likeMAKEFLAGS
). This will use the NUMJOBS envvar, if it exists; if it doesn't, automatically use-j4
. You can tune or rename it to your taste.(N.B.: Personally, I'd prefer the default to be
-j1
or""
, especially if it's going to be distributed to others, because although I have multiple cores also, I compile on many different platforms, and often forget to dis-able the-jX
setting.)I usually do this as follows in my bash scripts:
Aliases are not expanded inside scripts. It's better to create a separate
make
script and place it into one of the$PATH
directories:On Ubuntu 16.4 using all CPU cores:
or