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 3351382
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:54:07+00:00 2026-05-18T01:54:07+00:00

I’ve got a common set of make rules being used in multiple directories. Each

  • 0

I’ve got a common set of make rules being used in multiple directories. Each make is very simple:

BASEDIR:=../..
TARGET:=theTarget
include Makefile.include

Unless overridden, I have a default value for the source code:

SRC:= $(wildcard *.cpp)

We want to build the object files in a directory (not with the sources), so:

OBJDIR:= ../obj
BARE:= $(foreach f,$(SRC),$(notdir $(f) )
OBJ:= $(foreach o,$(BARE),$(OBJDIR)/$(o).o )

So I have the .o files, but I need each one to fire a rule based on the .cpp
As long as the directory is known, this is easy:

$(OBJDIR)/%.o: %.cpp

but in some of the directories, there are arbitrarily grouped files in subdirectories. I temporarily got around this by hardcoding all of them in my central makefile:

$(OBJDIR)/%.o: a/%.cpp
    build...
$(OBJDIR)/%.o: b/%.cpp
    build...

But what I’d like to do is be able to specify the build directories in a variable if possible. Is there any way to set up the dependencies

$(OBJDIR)/x.o: a/x.cpp
$(OBJDIR)/y.o: a/y.cpp

and

$(OBJDIR)/z.o: b/z.cpp

without manually having to list out the dependencies?

For that matter, I also have dependency files (.d)

$(DEPDIR)/%.d: $(SRC)

I want to set up dependencies based on whatever is in the source.
For example:
.deps/x.d: a/x.cpp
g++ -MM -MT$(objdir)/$(notdir $@) $< -o $(DEPDIR)/$(notdir $@ )

Is there an easy way to generate these rules without writing them for each directory?

Here’s where I got the information on generating the .d files in the first place:
http://mad-scientist.net/make/autodep.html#advanced

The goal in the end is to include all the .d files and have dependence tree auto-generated from the files. But that is the last part. I don’t know how to use the .d files. Do I just include them all?

include $(DEPDIR)/%.d

doesn’t work.

I can cat them all together in a rule:
cat $(DEP) >$(DEPDIR)/deps.inc

and then

include deps.inc
  • 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-18T01:54:07+00:00Added an answer on May 18, 2026 at 1:54 am

    First, to cope with the source directories:

    vpath %.cpp a b
    
    $(OBJDIR)/%.o: %.cpp
        build...
    

    Then if you want you can generate the dependency files like this:

    # Might as well use DEPDIR, rather than "deps"
    
    $(DEPDIR)/%.d: %.cpp 
        g++ -MM ...
    
    # Note the wildcard. And the '-' at the beginning means
    # "don't panic if there aren't any".
    
    -include $(DEPDIR)/*.d
    

    But there is a better way which is described in the source you cite. Don’t make a rule for the .d files, just build them in the same rule that builds the object files. That way you don’t build them unless you actually need them.

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

Sidebar

Related Questions

No related questions found

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.