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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:12:58+00:00 2026-05-12T11:12:58+00:00

I have a C++ small project using GNU Make. I’d like to be able

  • 0

I have a C++ small project using GNU Make. I’d like to be able to turn the following source files:

src/
  a.cpp
  b/
    b.cpp
  c/
    c.cpp

into the following output structure (I’m not concerned about duplicates at this point):

build/
  a.o
  b.o
  c.o

So far I have the following, which unfortunately puts the .o and .d right next to each .cpp:

OBJS            :=      $(foreach file,$(SRCS),$(file).o)
DEPS            :=      $(patsubst %.o,%.d,$(OBJS))
sinclude $(DEPS)

$(OBJS) : %.o : %.cpp
        @echo Compiling $<
        $(CC) $(CC_FLAGS) $(INCS) -MMD -o $@ $<  

I’m aware of the $(notdir …) function, but at this point my efforts to use it to filter the objects has failed. Can anyone shed some light on this? It seems like fairly reasonable thing to do.

  • 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-12T11:12:58+00:00Added an answer on May 12, 2026 at 11:12 am

    There are at least two ways you can do this. First (and what I’d recommend) is you can add the build directory to the target names (even when using a pattern rule). For example:

    $(OBJS) : build/%.o : %.cpp
    

    Second, you can use the VPATH variable to tell make to search a different directory for prerequisites. This is probably the more commonly (over) used approach. It has at least one serious drawback, and that is if you go with it, and later run into problems with “duplicates”, there’s no way to solve the problem. With the former approach, you can always mirror the source directory structure underneath the build directory to avoid duplicates clashing.

    Edit: My previous answer was a little short on detail, so I will expand upon it to show that this actually works as advertised. Here is a complete working example Makefile that uses the first technique described above to solve the problem. Simply paste this into a Makefile and run make — it will do the rest and show that this does in fact work.

    Edit: I can’t figure out how to get SO to allow tab characters in my answer text (it replaced them with spaces). After copying and pasting this example, you’ll need to convert the leading spaces in the command scripts into tabs.

    BUILD_DIR := build
    
    SRCS := \
        a.c \
        b.c \
        c.c \
        a/a.c \
        b/b.c \
        c/c.c
    
    OBJS := ${SRCS:%.c=${BUILD_DIR}/%.o}
    
    foo: ${OBJS}
        @echo Linking $@ using $?
        @touch $@
    
    ${BUILD_DIR}/%.o: %.c
        @mkdir -p $(dir $@)
        @echo Compiling $< ...
        @touch $@
    
    ${SRCS}:
        @echo Creating $@
        @mkdir -p $(dir $@)
        @touch $@
    
    .PHONY: clean
    clean:
        rm -f foo
        rm -f ${OBJS}
    

    In particular, note that there are source files with duplicate names (a.c and a/a.c, b.c and b/b.c, etc) and that this doesn’t cause any problems. Also note there is no use of VPATH, which I recommend to avoid using due to its inherent limitations.

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

Sidebar

Ask A Question

Stats

  • Questions 202k
  • Answers 202k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Adding authentication mechanisms to an EJB Container is usually a… May 12, 2026 at 8:25 pm
  • Editorial Team
    Editorial Team added an answer There is just one way to make the window manager… May 12, 2026 at 8:25 pm
  • Editorial Team
    Editorial Team added an answer Update: This solution is primarily applicable to versions R2014a and… May 12, 2026 at 8:25 pm

Related Questions

I have a C++ small project using GNU Make. I'd like to be able
Imagine you're free to choose a tool like GNU make for a new C++
I'm looking for recommendations for open-source projects written in C++ that will help me
I think I'm in somewhat of a unique situation: I have a decent amount
Does anyone have experience using makefiles for Visual Studio C++ builds (under VS 2005)

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.