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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:28:07+00:00 2026-05-20T10:28:07+00:00

I have a makefile which does the usual directory creation: $(Release_target_OBJDIR)/%.o: %.cpp mkdir -p

  • 0

I have a makefile which does the usual directory creation:

$(Release_target_OBJDIR)/%.o: %.cpp
     mkdir -p $(dir $@)
     $(COMPILE.cpp) $< $(CFLAGS) $(INCLUDES) -o $@

Unfortunately when I run this under scratchbox2 the mkdir -p command always fails silently.

I attempted the following kludge which doesn’t work:

$(Release_target_OBJDIR)/%.o: %.cpp
    mkdir $(dir $(dir $(dir $@)))
    mkdir $(dir $(dir $@))
    mkdir $(dir $@)
    $(COMPILE.cpp) $< $(CFLAGS) $(INCLUDES) -o $@

This outputs:

mkdir -p /home/foo/projects/htc/arm/obj/cbar/release/                  
mkdir -p /home/foo/projects/htc/arm/obj/cbar/release/                  
mkdir -p /home/foo/projects/htc/arm/obj/cbar/release/  

… the trailing slash prevents the dir function from stripping the last directory in the way I wanted.

Short of writing a script or small C app to replicate the “-p” functionality, does anyone have any ideas for creating the subdirectories within the makefile?

Without the -p option mkdir will give an error when the makefile tries to create a directory which already exists. I can do mkdir blah 2> /dev/null but then I risk losing other error messages.

Does anyone have any thoughts as to why mkdir -p doesn’t work under scratchbox2?

EDIT

Based on suggestions by bobbogo I put this together. It looks fairly convoluted, but seems to work, even under scratchbox2.

# Generic variables for use in functions
comma:= ,
empty:=
space:= $(empty) $(empty)

# Make directory function
forlooprange = $(wordlist 1,$(words $1),1 2 3 4 5 6 7 8 9 10)
forloop = $(foreach n,$(call forlooprange,$1),$(call $2,$n,$3))
mkdirfunc0 = test -d $1 || mkdir $1;
mkdirfunc1 = $(call mkdirfunc0,/$(subst $(space),/,$(foreach n,$(wordlist 1,$1,$2),$n)))
mkdirfunc2 = $(call forloop,$1,mkdirfunc1,$1)
mkdirmain = $(call mkdirfunc2,$(subst /, ,$1))

.PRECIOUS: %/.sentinel  
%/.sentinel:
    $(call mkdirmain,$*)
    touch $@
  • 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-20T10:28:07+00:00Added an answer on May 20, 2026 at 10:28 am

    You can replace your forest of mkdirs with this:

    $(Release_target_OBJDIR)/%.o: %.cpp
        $(foreach d,$(subst /, ,${@D}),mkdir $d && cd $d && ):
        ∶
    

    This will create a shell command somethng like this:

    mkdir projects && cd projects && mkdir htc && cd htc && mkdir arm && cd arm && :
    

    This runs for every compile. Not very elegant. You could optimise this by using some sort of sentinel file. For instance:

    $(Release_target_OBJDIR)/%.o: %.cpp ${Release_target_OBJDIR}/.sentinel
        ∶
    
    %/.sentinel:
        $(foreach d,$(subst /, ,$*),mkdir $d && cd $d && ):
        touch $@
    

    .sentinel gets created once before all objects, and is make -j friendly. In fact you should do it this way even if mkdir -p works for you (in which case you would use mkdir -p rather than the $(foreach) hacksolution).

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

Sidebar

Related Questions

I have a source directory which uses makefile to compile the code. This makefile/configure
I have a makefile in a directory of mine which builds scripts with certain
I have a makefile which looks for .txt files in a directory and for
We have a Makefile which runs on a Windows 2003 machine and we are
So, I have a Makefile which runs different commands of how to build S/W.
I have a Makefile from which I want to call another external bash script
I'm new to the world of Makefile writing. I have a C project which
I have a large makefile which builds several libraries, installs them, and then keeps
I have an MSVC Makefile Project in which I need to set an environment
I have a lib directory in my applications main directory, which contains an arbitrary

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.