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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:49:22+00:00 2026-05-12T17:49:22+00:00

I know I am doing it wrong, but I can’t figure out how to

  • 0

I know I am doing it wrong, but I can’t figure out how to organize this makefile. I define my util source files, and use some functions to define the .o files from them here:

UTIL_SRC = utils/src/foo.cpp utils/src/bar.cpp utils/src/baz.cpp

UTIL_OBJS = $(patsubst utils/src/%.cpp,utils/obj/%.o,$(UTIL_SRC))

This is the target that I use these files for:

lib : lib/libutils.a

lib/libutils.a : $(UTIL_OBJS)
    rm -f lib/libutils.a
    ar -c -q lib/libutils.a $(UTIL_OBJS)

Then, when I get to the rule to compile these babies, I would love to just have one command that would iterate through each UTIL_OBJS file and each UTIL_SRC file. Instead I have resorted to this monstrosity, which defeats the purpose of storing them in variables.

$(UTIL_OBJS) : $(UTIL_SRC)
    g++ $(UTIL_FLAGS) utils/src/foo.cpp -o utils/obj/foo.o
    g++ $(UTIL_FLAGS) utils/src/bar.cpp -o utils/obj/bar.o
    g++ $(UTIL_FLAGS) utils/src/baz.cpp -o utils/obj/baz.o

Can I condense this down to one line? How? Thanks, great ones!

  • 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-12T17:49:23+00:00Added an answer on May 12, 2026 at 5:49 pm

    It’s usually easier to work with implicit rules. There are a lot of predefined ones, where you’ll only need to specify variables.

    CXX=g++
    CXXFLAGS=$(UTIL_FLAGS)
    

    Then you need to define an executable, like this

    myutil: $(UTIL_OBJS)
    

    Since you’re not storing your objects in the same directory, you’ll need to specify a new implicit rule as well though (otherwise, we’d be done now).

    utils/obj/%.o: utils/obj/%.cpp
    

    % is a pattern-match, it’ll match the same text on both left and right side, so this rule will make foo.o out of foo.cpp.
    Try if that’ll work without the command (it might have grabbed that from another rule, I’m not sure), otherwise let it say:

    utils/obj/%.o: utils/obj/%.cpp
        $(CXX) $(CXXFLAGS) -o $@ $^
    

    $@ is the target of the rule (e.g. foo.o), and $^ is all files on the right hand side.
    I’m writing this off the top of my head, without the possibility to test it, so please let me know how it turned out.. 🙂

    To make it even more elegant, you can include a dependency file

    include .depend
    

    If you’re running GNU make, it’ll try to make the .depend file if it can’t find it (with old school make, you need to create it yourself first, it can be just a dummy though, if you’d like to manage it through the makefile)

    .depend: $(UTIL_SRC)
        $(CXX) -MM -o $@ $^
    

    The dependency file will contain lines for each .cpp file, telling make what header files it needs, which will allow make to recompile the necessary files when you change something. This doesn’t help with your original question though, just thought it might come in handy.

    EDIT:
    As a response to your edit. You could probably drop the commands for creating the .a-file as well, that too is already available as an implicit rule. Not sure exactly how it works though, haven’t used it much. I do know that there are a bunch of quirks in make for dealing with .a(rchive?)-files.

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

Sidebar

Related Questions

Okay, I know I'm doing something wrong - but can't figure out a better
I know almost nothing about linq. I'm doing this: var apps = from app
I know I can get the public static members of a class by doing
I'm doing some funky authentication work (and yes, I know, open-id is awesome, but
OK. This problem is doing my head in. And I don't know if there
I know the regex for doing a global replace, %s/old/new/g How do you go
I know that just using rand() is predictable, if you know what you're doing,
Does anybody know if there is a way to understand what users are doing
I've been doing a lot of reading about AJAX, and wanted to know which
Currently, I only know a way of doing RPC for POJOs in Java, and

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.