19

Is there a way that I can make

$ make

default to:

$ make -j 8

?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
anon
  • 41,035
  • 53
  • 197
  • 293

9 Answers9

73

Set the environment variable MAKEFLAGS to -j 8. If you are using csh or tcsh, you can do this with setenv MAKEFLAGS '-j 8'. If you are using bash, you can do this with export MAKEFLAGS='-j 8'. You might wish to put this command in your shell's start-up file, such as .cshrc or .bashrc (in your home directory).

Caution: Setting a default like this will apply to all invocations of make, including when you "make" a project other than your own or run a script that invokes make. If the project was not well designed, it might have problems when it is built with multiple jobs executing in parallel.

s4y
  • 50,525
  • 12
  • 70
  • 98
Eric Postpischil
  • 731
  • 1
  • 4
  • 2
12

The answers suggesting alias make='make -j 8' are fine responses to your question.

However, I would recommend against doing that!

By all means, use an alias to save typing - but call it something other than make.

It might be OK for whatever project you're currently working on; but it's quite possible to write makefiles with missing dependencies which do not quite work properly with -j, and if you encounter such a thing, you'll be left wondering why the build fails in a mysterious way for you but works fine for other people.

(That said, if you do alias make, you can get bash to ignore the alias by typing \make.)

Matthew Slattery
  • 45,290
  • 8
  • 103
  • 119
  • 1
    Why not just suggest using the MAKEFLAGS environment variable like Eric says? – xaxxon Oct 31 '15 at 10:05
  • @xaxxon: aliases are effective only in interactive shells, (i.e., sourced files or manually typing `make` in a terminal), while `MAKEFLAGS` will affect all scripts and programs that use `make`, which may or may not be what you want. – MestreLion Jun 24 '22 at 10:10
10

alias make="make -j 8", assuming bash shell

David Wolever
  • 148,955
  • 89
  • 346
  • 502
Anycorn
  • 50,217
  • 42
  • 167
  • 261
  • 3
    wait .. how does this not recursively call make i.e. make -> make -j 8 -> make -j 8 -j 8 -> make -j 8 -j 8 -j 8 – anon Jan 28 '10 at 01:08
  • 7
    The alias changes the make command to default 'make -j 8' It does not call the make command. – Gazler Jan 28 '10 at 01:11
  • This can also be placed in the .bashrc file so you don't have to enter it into each shell, .bashrc is in your home directory (in Debian at least). – Grundlefleck Jan 28 '10 at 01:52
4

Add

MAKEOPTS='-j8'
MAKEFLAGS='-j8'

to /etc/make.conf (create it if it doesn't already exist).

If that doesn't work, add

export MAKEOPTS='-j8'
export MAKEFLAGS='-j8'

to your system-wide profile (e.g., /etc/profile).

For me, MAKEOPTS alone didn't work. Possibly MAKEFLAGS is all that's needed.

Geremia
  • 4,745
  • 37
  • 43
  • Just so you would know, can't get this to work either. https://gist.github.com/sanmai/dcc31ae20afa6e8ba4721f174fe05fd9 – sanmai Feb 02 '18 at 00:23
  • Check the gist to see for yourself. `MAKEFLAGS` gets to keep only `-j`, without a digits – sanmai Feb 02 '18 at 04:06
  • @sanmai When I run that Makefile with GNU Make 4.2.1 (my `strings /usr/bin/make | grep MAKEOPTS` also outputs nothing), I get `' -j4 --jobserver-auth=3,4' must have --jobs=4`. – Geremia Feb 02 '18 at 04:41
  • Well, it seems like [they added this feature in 4.2](https://lists.gnu.org/archive/html/info-gnu/2016-05/msg00013.html). There's no easy way to get 4.2 in Debian now... – sanmai Feb 05 '18 at 01:23
3

If you are using the command line you can do:

alias make='make -j 8'

This will be temporary, to make it permanent you need to add it to .bashrc

Read here: http://www.linfo.org/make_alias_permanent.html

Gazler
  • 83,029
  • 18
  • 279
  • 245
1

Why not create an outer makefile, that calls another makefile like this, this is replicated from the manual here.

     SUBDIRS = foo bar baz
     .PHONY: subdirs $(SUBDIRS)
     subdirs: $(SUBDIRS)
     $(SUBDIRS):
             $(MAKE) -j 8 -C $@

     foo: baz
t0mm13b
  • 34,087
  • 8
  • 78
  • 110
1

If you're using GNU make:

$ make --version
GNU Make 4.2.1
Built for x86_64-alpine-linux-musl

...then I found that GNUMAKEFLAGS seems to work:

export GNUMAKEFLAGS=-j8
make

Disclaimer, I'm a n00b with the C toolchain so sorry if this isn't portable. It's working on Alpine Linux 3.8 (in Docker) though.

Tom Saleeba
  • 4,031
  • 4
  • 41
  • 36
0

Relatively new GNU make versions (>=4.3?) support this inside the makefile:

MAKEFLAGS += -j

Note: this is not setting the environment variable before starting make, but setting the internal make variable inside the makefile. If for some reason you need to override this option and build in a single thread, make -j1 overrides the option set inside the makefile.

vjalle
  • 549
  • 4
  • 13
-1

You could move /usr/bin/make to /usr/bin/make_orig and make /usr/bin/make this script:

#!/bin/sh

/usr/bin/make_orig -j 8 $@

Be sure to run chmod +x /usr/bin/make.

Try this method if the simpler method in my other answer doesn't work. This method isn't as safe and is a bit of a kludge.

Geremia
  • 4,745
  • 37
  • 43
  • This is just plain bad. Imagine if you ever need to update your make binary. – sanmai Feb 01 '18 at 09:11
  • @sanmai Yes, MAKEOPTS is much better; cf. [my other answer above](https://stackoverflow.com/questions/2151605/make-make-default-to-make-j-8/42658101#42658101). – Geremia Feb 01 '18 at 15:11