6

After reading answer to this question: Make "make" default to "make -j 8"

I am wondering if there is way to make the -j option automatically use the correct number of compile threads?

So I say make. And the make command itself uses 6 or 4 or 8 threads depending on the hardware?

Community
  • 1
  • 1
Ahmad Mushtaq
  • 1,395
  • 1
  • 10
  • 32
  • 1
    What do you mean with _correct number of compile threads_? – maba Jun 08 '12 at 08:28
  • @maba usually the recommendation is to use the number of cpu cores x 2. So for a a dual core cpu, it should be -j 4. But some recommend as many compile threads as cores. So -j 2 for a dual core cpu. – Ahmad Mushtaq Jun 08 '12 at 09:43
  • Also related: http://stackoverflow.com/q/4778389/946850 – krlmlr Nov 18 '13 at 15:02

1 Answers1

15

make does not look up the number of cores by itself if you just use make -j -- instead, it parallelizes to the max. However, you should be able to determine the number of cores by

grep -c "^processor" /proc/cpuinfo

or (as per Azor-Ahai's comment, if available on your system)

nproc

Hence:

make -j $(nproc)

See "How How to obtain the number of CPUs/cores in Linux from the command line?" for more details. Also, see GNU make: should the number of jobs equal the number of CPU cores in a system?

Community
  • 1
  • 1
krlmlr
  • 25,056
  • 14
  • 120
  • 217