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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:13:31+00:00 2026-06-09T14:13:31+00:00

My makefile contains the following lines 11 SRC := $(shell echo src/*.cpp) 12 SRC

  • 0

My makefile contains the following lines

 11 SRC := $(shell echo src/*.cpp)
 12 SRC += $(shell echo $(TEST_ROOT)/*.cpp)
 13 
 14 OBJECTS = $(SRC:.cpp=.o)

 22 # [debug] target
 23 $(TARGET): $(OBJECTS)
 24   $(CC) $(OBJECTS) -o $(TARGET)
 25 
 26 # [debug] .o
 27 $(OBJECTS): $(SRC)
 28   $(CC) $(CFLAGS) $(DEBUG_FLAGS) -c $(SRC) $(OBJECTS)

Which fails because … well the following evidently is not a way to do it

g++ -pedantic -Wall -Werror -O0 -g -Wno-format -DUSE_MEASURE -c src/Timer.cpp test/TimerTest.cpp src/Timer.o test/TimerTest.o

How should i modify line #28 to produce .o from all my sources please?

  • 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-06-09T14:13:32+00:00Added an answer on June 9, 2026 at 2:13 pm

    This should do the trick:

    $(OBJECTS): %.o : %.cpp
        $(CC) $(CFLAGS) $(DEBUG_FLAGS) -c $< $@
    

    This rule will build one object file, and Make will call it once for each object that the other rule needs. You can write a rule that will build all object files in one pass, but there’s no advantage to that.

    EDIT:

    Suppose SRC is src/foo.cpp src/bar.cpp testroot/baz.cpp,

    so OBJECTS is src/foo.o src/bar.o testroot/baz.o

    We could have a separate rule for each:

    src/foo.o : src/foo.cpp
        ...
    
    src/bar.o : src/bar.cpp
        ...
    
    testroot/baz.o : testroot/baz.cpp
        ...
    

    But that’s redundant (since the commands are very similar) and inconvenient (since we don’t want to add/remove rules whenever we add/remove targets). So we use a pattern rule:

    %.o : %.cpp
        ...
    

    Now when Make wants to build src/foo.o, it sees that the target matches the pattern %.o, the stem (corresponding to %) is src/foo, and the corresponding prereq, src/foo.cpp exists, so it uses this rule.

    But we have a variable OBJECTS that lists the targets we want to build with this rule, so we can restrict it to a static pattern rule, which looks about the same but has some advantages we need not get into here:

    $(OBJECTS) : %.o : %.cpp
        ...
    

    Now for the ... part. The command tells the compiler to scan the prereq (src/foo.cpp) and build the target (src/foo.o), so we must refer to those things in the command. Automatic variables help with that; $@ is the name of the target, and $< is the name of the first (and in this case the only) prerequisite:

    $(OBJECTS): %.o : %.cpp
        $(CC) $(CFLAGS) $(DEBUG_FLAGS) -c $< $@
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a Makefile(code below) for my program which contains hello.h, hello.cpp and main.cpp
A common issue automake complains about is caused by following lines in various Makefile.am:s
I have a C project that has the following structure Main/ Makefile.am bin/ src/
My makefile contains these snippets (among others): SRC = src OBJ = obj DEPS
I made some modifications to SVG Edit which contains a make file. When I
I'm creating a Makefile for a project. I have the following structure of Makefiles:
I have the following makefile for my project, and I'd like to configure it
I have tried putting the following in my Makefile: @if [ $(DEMO) -eq 0
I have a class 'Vector3' which is compiled successfully. It contains both non-friend and
I am experimenting with makefile...mainly when sources are in many directories I have following

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.