Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 696901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:06:46+00:00 2026-05-14T03:06:46+00:00

Not sure if this is possible in one Makefile alone, but I was hoping

  • 0

Not sure if this is possible in one Makefile alone, but I was hoping to write a Makefile in a way such that trying to build any target in the file auto-magically detects the number of processors on the current system and builds the target in parallel for the number of processors.

Something like the below “pseudo-code” examples, but much cleaner?

all:
    @make -j$(NUM_PROCESSORS) all

Or:

all: .inparallel
    ... build all here ...

.inparallel:
    @make -j$(NUM_PROCESSORS) $(ORIGINAL_TARGET)

In both cases, all you would have to type is:

% make all

Hopefully that makes sense.

UPDATE: Still hoping for an example Makefile for the above. Not really interested in finding the number of processes, but interested in how to write a makefile to build in parallel without the -j command line option.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:06:46+00:00Added an answer on May 14, 2026 at 3:06 am

    After poking around the LDD3 chapter 2 a bit and reading dmckee’s answer, I came up with this not so great answer of using two makefiles (I would prefer just one).

    $ cat Makefile
    MAKEFLAGS += -rR --no-print-directory
    
    NPROCS := 1
    OS := $(shell uname)
    export NPROCS
    
    ifeq ($J,)
    
    ifeq ($(OS),Linux)
      NPROCS := $(shell grep -c ^processor /proc/cpuinfo)
    else ifeq ($(OS),Darwin)
      NPROCS := $(shell system_profiler | awk '/Number of CPUs/ {print $$4}{next;}')
    endif # $(OS)
    
    else
      NPROCS := $J
    endif # $J
    
    all:
        @echo "running $(NPROCS) jobs..."
        @$(MAKE) -j$(NPROCS) -f Makefile.goals $@
    
    %:
        @echo "building in $(NPROCS) jobs..."
        @$(MAKE) -j$(NPROCS) -f Makefile.goals $@
    $ cat Makefile.goals
    MAKEFLAGS += -rR --no-print-directory
    NPROCS ?= 1
    
    all: subgoal
        @echo "$(MAKELEVEL) nprocs = $(NPROCS)"
    
    subgoal:
        @echo "$(MAKELEVEL) subgoal"
    

    What do you think about this solution?

    Benefits I see is that people still type make to build. So there isn’t some “driver” script that does the NPROCS and make -j$(NPROCS) work which people will have to know instead of typing make.

    Downside is that you’ll have to explicitly use make -f Makefile.goals in order to do a serial build. And I’m not sure how to solve this problem…

    UPDATED: added $J to above code segment. Seems work work quite well. Even though its two makefiles instead of one, its still quite seamless and useful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Not sure this is possible, but looking to write a script that would return
I'm not sure this is possible, but is there a way, using template programming
I'm not sure this is even possible, but let's see if one of you
I'm not sure if this is possible(hoping so). I have a dataset that I
I'm not sure if this is possible in one mysql query so I might
I'm not sure if this possible but probably is fairly simple. I've made a
I'm not sure this is possible, but in ruby, you can dynamically call a
I'm not sure this is possible, but is there a syntax to be used
I'm not sure this is even possible but I know if it is, the
I'm not sure if this is possible (complete non-flash developer speaking), but we have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.