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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:54:36+00:00 2026-05-15T15:54:36+00:00

I have the following Makefile rules: DIR = src SOURCES = $(shell find $(DIR)

  • 0

I have the following Makefile rules:

DIR = src
SOURCES = $(shell find $(DIR) -name "*.cpp")
OBJS := $(SOURCES:.cpp=.o)

With this definition all my .o files are located in the same directories (and sub-directories) as their .cpp counterparts. Such allocation principle turns the project directory into a mess very soon (with 50+ files). I would like to make a new specific folder for .o files and place them there. I need to write a make-rule that will convert every .o file name like the following:

foo/bar/test.o —> objects/foo-bar-test.o

How can I create such a rule in Makefile? Thanks in advance!

  • 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-15T15:54:36+00:00Added an answer on May 15, 2026 at 3:54 pm

    I’m not sure if you can specify patterns with these modification directly, but you always can use macros (at least in GNU make). An example could be this:

    define export_rule
    objects/$(patsubst %.cpp,%.o,$(subst /,-,$(1))) : $(1)
        <<your rules here>>
    endef
    
    
    SOURCES = $(shell find $(DIR) -name "*.cpp")
    
    $(foreach s,$(SOURCES), $(eval $(call export_rule,$(s))))
    

    This defines the exact rules you want, avoiding (as some solutions proposed above) the name clashes in case two files with the same name reside in different directories.

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

Sidebar

Related Questions

Utility: NMake Platform : Windows 7 I have the following Makefile FILE = $(shell)
I am experimenting with makefile...mainly when sources are in many directories I have following
I have the following makefile: OUTPUTDIR = build all: v12target v13target v12target: INTDIR =
I want to just type 'make all' and have the following Makefile do everything
I have the following Makefile: OBJDIRS = Runtime/Core/Common Runtime/Core/Graphic/SymbolXLib Runtime/Core/Map Runtime/Core/SymbolDictionary \ Runtime/CoreClient/RuntimeCoreJava OBJS
I have the following Makefile.am inside ./src where I need to generate dependencies for
I have a simple assembly program with the following makefile: all : calc calc
I currently have the following rule in a Makefile: ../obj/%.o: %.cpp mkdir -p ../obj/$<
I have the following makefile (fragment) SRC_DIR = src OBJ_DIR = obj DEP_DIR =
I have the following piece of makefile: CXXFLAGS = -std=c++0x -Wall SRCS = test1.cpp

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.