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

  • Home
  • SEARCH
  • 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 6730801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:27:08+00:00 2026-05-26T10:27:08+00:00

My makefile looks something like this: FOO_OBJECT_FILES := $(OBJDIR)/Foo.cpp.o BAR_OBJECT_FILES := $(OBJDIR)/Bar.cpp.o $(OBJDIR)Bar.c.o ALL_OBJECT_FILES

  • 0

My makefile looks something like this:

FOO_OBJECT_FILES := $(OBJDIR)/Foo.cpp.o
BAR_OBJECT_FILES := $(OBJDIR)/Bar.cpp.o $(OBJDIR)Bar.c.o
ALL_OBJECT_FILES := $(FOO_OBJECT_FILES) $(BAR_OBJECT_FILES)

$(BINDIR)/Foo.a: $(FOO_OBJECT_FILES)
    # Rules for making a static library out of Foo's object files go here.

$(BINDIR)/Bar.a: $(BAR_OBJECT_FILES)
    # This uses the exact same command sequence as the previous rule.

$(BINDIR)/All.a: $(ALL_OBJECT_FILES)
       # Ditto.

# ...

When (not if) more targets are added to the project, the developer will have to update at least three things:

  1. The list of the new target’s object files
  2. The list of all object files
  3. Targets for making the new target, even if it uses the same rules as the others

Is there a way to simplify this process, or am I stuck with it?

I tried using wildcard rules, but it doesn’t look like they work with macros.

$(BINDIR)/%.a: $(%_OBJECT_FILES)
    # ...

You could treat the lists of object files as rules, but then the final target rules can’t access them directly.

OBJECT_FILES_Foo: $(OBJDIR)/Foo.cpp.o
OBJECT_FILES_Bar: $(OBJDIR)/Bar.cpp.o $(OBJDIR)Bar.c.o
OBJECT_FILES_All: FOO_OBJECT_FILES BAR_OBJECT_FILES

$(BINDIR)/%.a: OBJECT_FILES_%
    # This rule can't see into the object file lists to use them to build.

Is there no better way?

  • 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-26T10:27:08+00:00Added an answer on May 26, 2026 at 10:27 am

    There are probably plenty of ways to do this. One such way is the following. All that needs to be done for a new target is add its name to the list of modules, and give the list of dependencies for it.

    BINDIR := bin
    OBJDIR := obj
    
    MODULES := Foo Bar
    
    Foo_OBJS := $(OBJDIR)/Foo.cpp.o
    Bar_OBJS := $(OBJDIR)/Bar.cpp.o $(OBJDIR)/Bar.c.o
    
    #####################################################
    #                                                   #
    #   Nothing below here should need to be altered.   #
    #                                                   #
    #####################################################
    
    All_OBJS := $(foreach mod, $(MODULES),$($(mod)_OBJS))
    
    define rule
    $(BINDIR)/$(1).a: $($(1)_OBJS)
        @echo
        @echo 'Target: $$@'
        @echo 'Deps  : $$^'
    endef
    
    $(foreach lib, All $(MODULES), $(eval $(call rule,$(lib))))
    
    ###########################################
    #                                         #
    # The next part is just here for testing. #
    #                                         #
    ###########################################
    
    .PHONY: all
    all: $(foreach lib, All $(MODULES),$(BINDIR)/$(lib).a)
    
    %.o:
        @echo Making $@
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My project directory looks like this: /project Makefile main /src main.cpp foo.cpp foo.h bar.cpp
I have a Makefile.am file right now that looks like this: lib_LIBRARIES = foo.a
I have a makefile with rules that look like this: data/foo-trunk.xml: svn log -v
I have a Makefile that looks like this CXX = g++ -O2 -Wall all:
I'm trying to specify rpath in my binary. My makefile looks like this- CC=gcc
My source and build tree looks like this (see Makefile to put object files
My current Makefile for weaving looks something like: SUFFIXES: .tex .pdf .Rnw MAIN =
My Makefile looks like this: BIN = bin OBJECTS = object1.o \ object2.o \
I have a makefile that looks like: default: lua blah.lua Now, in Vim, I
I have a multi module maven project which looks something like: main component_one subcomponent_bob

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.