imitate "make -j" from within the Makefile

The shell command make -j runs a Makefile's commands in parallel, which is useful on modern multicore CPUs. Can such parallelism be invoked from within a Makefile?

From the manual for Gnu make, my best guess is using the main Makefile as only a wrapper, which passes -j to a sub-make that does the real work. But maybe there's some environment variable or dot file or internal variable or other ungooglable snoopy-swearing way to do this.

It's ok to limit the number of jobs, e.g,. -j $$(nproc).

Probably Linux, but not necessarily.

(Adding something like alias make='make -j' to ~/.bashrc is too dangerous; the decision about whether to parallelize should be per Makefile, not per user.)

1 Answer

You can append to MAKEFLAGS from within the makefile itself, for example:

MAKEFLAGS += --jobs=$(CPUS)

Where CPUS is defined in the makefile.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like