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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:43:02+00:00 2026-06-15T14:43:02+00:00

I have a following directory structure in my project: bin/ dist/ include/ ├── module_a/

  • 0

I have a following directory structure in my project:

bin/
dist/
include/
├── module_a/
└── module_b/
Makefile
src/
├── module_a/
└── module_b/

Folder include/ contains *.hpp‘s while *.cpp‘s are in src/. I would like to compile all sources to bin/ and then link them up together to dist/. Seems a pretty reasonable wish for me.

I would like to know the best practices for a Makefile for this case. All I can find is %.o: %.cpp target, but that doesn’t really work, because of different source and binary folder.

I was trying to use something like this:

D_SRC = src
D_BIN=bin

F_CPP   :=  $(shell find $(D_SRC) -iname '*.cpp' -type f)
F_OBJ   :=  $(shell echo $(F_CPP) | sed s:\ :\\n:g | sed s:$(D_SRC):$(D_BIN): | sed 's:^\(.*\)\.cpp$$:\1\.o:')

$(F_OBJ): $(F_SRC)                                                                  
    $(foreach file, $(F_SRC), \                                                     
        $(GXX) $(CXXFLAGS) -c $(file)\                                              
    )

This target doesn’t work, because $(F_OBJ) paths start with bin/, while foreach compiles sources to current working dir. I could make it compile to bin/, but that would happen only with a few more sed expressions and it’s ugly enough as it is.

It’s probably so difficult for me, because I don’t know make all that well, but I cannot be the only one with this project setup. In my opinion, it must be a pretty common one. I know I can write a Makefile for each module separately, but is that really the best choice here?

EDIT: I was now wondering what would I achieve with several Makefiles. If one was at root and another one in src/module_a, how would the latter know about the bin/? If you’d execute it with make -f src/module_a/Makefile, it would be the same as executing it from root directory, ’cause it’s working directory would be root. Another way, I guess, would be to change directory before executing it, like so: make -C include/module_a, but in that case, how would it find bin/? I wouldn’t want to have something like D_BIN = ../../bin in a Makefile.

  • 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-15T14:43:03+00:00Added an answer on June 15, 2026 at 2:43 pm

    What I normally do is have a Makefile in the src directory (which can be invoked from the top level Makefile if you like) and then use rules like this:

    D_BIN = ../bin
    $(D_BIN)/%.o: %.cpp
    

    You could also experiment with just a makefile in the top level dir, and use rules that look like this:

    D_BIN = bin
    D_SRC = src
    $(D_BIN)/%.o: $(D_SRC)/%.cpp
    

    but I have not used such rules, so I don’t know the pros/cons vs the way I normally do it. The way I normally do it works fine, I even have rules that build depends like so:

    $(D_BIN)/%.d: %.cpp 
    

    and the link rule would be like:

    ../dist/outexe: $(F_OBJ)
    

    Using a foreach is usually frowned upon because it does not make use of all the features built into normal makefile rules (i.e. there is no depends check on a per file basis, either you build everything or nothing), and as such foreach should only be used as a last resort, but in this case you will be able to get it to work without the foreach.

    In addition to this there are much easier ways to build your file lists, you don’t need to use the shell or sed.

    F_CPP = $(wildcard *.cpp)
    F_OBJ = $(F_CPP:.cpp=.o)
    

    Update: This is how I normally issue recursive makes:

    SUBDIRS = src
    .PHONY: $(SUBDIRS)
    all: $(SUBDIRS)
    
    $(SUBDIRS):
        @echo "Building $@..."
        $(MAKE) -C $@ $(MFLAGS)
    

    Then indeed in your submake, you would need to use ../bin for example.

    However with a project as simple as yours, you might be better off just having one makefile at the root level and using rules like this:

    D_BIN = bin
    D_SRC = src
    $(D_BIN)/%.o: $(D_SRC)/%.cpp
    

    recursive makefiles are ok (ok but not great) if you have a really complex directory structure, where you will be adding/removing/modifying new dir trees as time goes on. But for a simple project where you just want to have separate directories for code and objs, it is probably overkill.

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

Sidebar

Related Questions

I have a C project that has the following structure Main/ Makefile.am bin/ src/
I have the following directory structure of a project: Folder project in Eclipse: --folder
I have the following directory structure. root --src ---tests src contains the source &
I have a Python project with the following directory structure: project/ project/src/ project/src/somecode.py project/src/mypackage/mymodule.py
I have an Eclipse project with the following directory structure: MyProj/ src/main/java/ com.me.myproject.widgets Widget.java
I have a project which has the following directory structure. root --include ----module1 ----module2
I have the following directory structure: project/ .git/ ... app/ ... config/ initializers/ braintree.rb
Update : Here is the project. I have the following directory structure: RMI |
OK, I have the following directory structure (it's a django project): -> project -->
I have a Python project with following directory structure: /(some files) /model/(python files) /tools/(more

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.