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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:56:11+00:00 2026-06-06T22:56:11+00:00

I am trying to make a makefile which does the following: gets source files

  • 0

I am trying to make a makefile which does the following:

  • gets source files from the src directory
  • puts object files in the obj directory
  • puts binary files in the bin directory
  • puts release targets in the rel directory
  • puts debug targets in the dbg directory

The first problem i’m having is that my target-specific variables don’t seem to be working here is the makefile:

# Build Directories
src_dir=src
obj_dir=obj
bin_dir=bin

cc=cl
cc_flags=

# create the directory for the current target.
dir_guard=@mkdir -p $(@D)

# source files
src = MainTest.cpp

# object files - replace .cpp on source files with .o and add the temp directory prefix
obj = $(addprefix $(obj_dir)/$(cfg_dir)/, $(addsuffix .obj, $(basename $(src))))

release: cfg_dir = rel
release: executable

debug: cfg_dir = dbg
debug: cc_flags += -Yd -ZI
debug: executable

executable: $(bin_dir)/$(cfg_dir)/MainTest.exe

# build TwoDee.exe from all of the object files.
$(bin_dir)/$(cfg_dir)/MainTest.exe : $(obj)
    $(dir_guard)
    $(cc) -out:$@ $(obj) -link

# build all of the object files in the temp directory from their corresponding cpp files.
$(obj_dir)/$(cfg_dir)/%.obj : $(source_dir)/%.cpp
    $(dir_guard)
    $(cc) $(cc_flags) -Fo$(obj_dir)/$(cfg_dir) -c $<

When I run make debug I get:

make: *** No rule to make target `obj//MainTest.obj', needed by `bin//MainTest.exe'.

There is also something else wrong because if I remove the debug and release variables and hardcode cfg_dir to rel I then get:

make: *** No rule to make target `obj/rel/MainTest.obj', needed by `bin/rel/MainTest.exe'.  Stop.

So my object rule must also be wrong. I am new to make files so if anyone sees other things which are wrong, comments are welcome.

  • 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-06T22:56:13+00:00Added an answer on June 6, 2026 at 10:56 pm

    Target-specific variables are only available in the recipes, not in the rules. This is because the rules are parsed when the Makefile is read, and dependencies are deduced from that.

    To get your rules tailored for multiple possible values of cfg_dir, I suggest you look at the example in the eval section of the GNU Make manual. This explains things like this:

    release: $(bin_dir)/rel/MainTest.exe
    
    debug: cc_flags += -Yd -ZI
    debug: $(bin_dir)/dbg/MainTest.exe
    
    define template =
    
    # build TwoDee.exe from all of the object files.
    $(bin_dir)/$(cfg_dir)/MainTest.exe : $(obj)
        $$(dir_guard)
        $$(cc) -out:$$@ $(obj) -link
    
    # build all of the object files in the temp directory from their corresponding cpp files.
    $(obj): $(obj_dir)/$(cfg_dir)/%.obj : $$(src_dir)/%.cpp
        $$(dir_guard)
        $$(cc) $$(cc_flags) -Fo$(obj_dir)/$(cfg_dir) -c $$<
    
    endef
    $(foreach cfg_dir,rel dbg,$(eval $(template)))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a deploy command in my Makefile, which simply overwrites to
I'm having trouble with trying to use make to place object files in a
I am trying to run make on a PintOS makefile but I keep getting
I am fairly new to iOS development and trying make a simple app which
Trying to make the infamous checkall checkbox for dynamically created rows from a MySQL
I need to write a pre-build makefile which is called separately from the main
I'm trying to compile a linux kernel module using a Makefile: obj-m += main.o
I have a small set of source code files for C. I'm trying to
I am trying to compile the Hello example in directory lib/Transforms/Hello , which is
I have the following makefile which is a slight modification on others that I

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.