How to make in parallel

Building the source code of big projects can take a lot of time. By default, the make command builds one target at a time. Since most of our computers have multiple cores, this process can be sped up by building multiple targets in parallel. This can be done easily by using the --jobs or -j parameter.

  • make -j will try to build all targets in parallel whose dependencies are available. This will swamp your machine with build tasks.
  • make -j 6 will try to build a maximum of 6 (or any non-zero positive integer you specify) targets in parallel. The actual number of builds done in parallel might be less depending on how many targets have their dependencies ready.
  • If you use parallel make frequently, then it is a good idea to turn it into an alias in your shell.
  • The -j option can be set in the MAKEFLAGS environment variable, if you want parallel make to be used whenever make is invoked. This is better than an alias since parallel make is used even if make is called inside programs which are not using (or not aware of) your alias.
  • You may want to use the same alias or MAKEFLAGS variable across machines with varying number of CPU cores. One way to solve this problem is to use the output of the nproc --all command to set the number of parallel jobs.

One thought on “How to make in parallel

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.