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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:28:13+00:00 2026-05-13T05:28:13+00:00

My project needs temporary directories which are created during the build using mkdir -p

  • 0

My project needs temporary directories which are created during the build using mkdir -p similarly to this:

all: dirtree $(OBJFILES)

dirtree: 
  @mkdir -p $(BUILD)/temp_directory

But this approach cannot be used with the -j switch, because first of the OBJFILES get compiled before the mkdir target is made.

Is there a standard way to do this?

  • 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-13T05:28:14+00:00Added an answer on May 13, 2026 at 5:28 am

    The problem with your makefile is that creation of your object files does not depend on creation of the relevant directories (only a phony “all” target does). This kind of dependency is necessary for -j option, and even without it your makefile works only by chance. There are two (right) ways to impose the dependency in question.

    Directories as separate targets

    You created the target for directory creation; what left is just put it as a prerequisite to object file rule:

    $(BUILD)/temp_directory/%.o: %.c   |   dirtree
            $(CC) $^ -o $@
    

    The pipe symbol | means that dirtree is an “order only prerequisite”. It is used when “dirtree” is a prerequisite but changes in the dirtree do not invalidate object files and do not affect the outcome of compilation command.

    Use of “order-only” prerequisite is important here. The thing is that dirtree target would be remade at each Make invocation. That would cause everything that depends on it be remade as well, so it would rebuild all object files every time.

    Create directories in shell commands

    Another way is to ensure that the directory is created immediately before you invoke compilation

    $(BUILD)/temp_directory/%.o: %.c
            @mkdir -p $(@D)
            $(CC) $^ -o $@
    

    Note the usage of $(@D). This is expanded as “the directory for the target file”. So it may be used uniformly in many places, and even with aid of a variable.

    Mkdir=@mkdir -p $(@D)
    $(BUILD)/temp_directory/%.o: %.c
            $(Mkdir)
            $(CC) $^ -o $@
    $(INSTALL_DIR)/%: src_dir/%
            $(Mkdir)
            cp -p $^ $@
    

    Both ways ensure that the directory is created before the compilation commands are invoked. Both ways require you to write some text (either | dirtree or $(Mkdir)) at each rule that needs it. Both ways are -j compatible, but the second solution requires mkdir -p to be thread-safe (as two such commands at once may try to create the same directory, and one of them would fail).

    While most systems implement it in such a way that mkdir -p is more or less thread safe, on some systems (as in some Solaris systems, for example), they are less thread-safe than the others. However, even in GNU toolchain mkdir -p may fail if they simultaneously invoke the same mkdir(2) library call.

    If you want to be very safe, you can work this around as well. What could be the problem? That two mkdir -p scripts try to create the same directory, and clash somewhere inside C library. Then, one of these mkdir-s will succeed, and the other will fail. However, if the mkdir you invoked failed, then it could be thread-unsafety-related failure only if the directory had been created by a concurrent mkdir. So it would be enough to just check that the target directory is created after mkdir invocation:

    Mkdir=@mkdir -p $(@D) || test -d $(@D)
    

    (This solution also has an issue with mode: mkdir may fail when directory exists, but doesn’t conform to umask, so you might want to check that as well. But that’s too much I guess.)

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

Sidebar

Related Questions

I have a project that needs to be done using ASP.NET. Currently, I'm using
I'm working on a project which needs to use youtube api. The html and
In my current project I'm using classes which implement the following ITransaction interface shown
I am using the Grails framework for a project, which uses Hibernate for ORM.
I want to start using GitHub Pages for my project's website. This simply requires
My database project needs to include a SQL file with a command that will
My index.html page for my project needs some Ruby code so I need to
In VS, it's simple. Everything the project needs is stored in the project folder
I have a project that needs to be deployed on multiple servers. Its the
I have a Java project that needs a addon interface. I was thinking about

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.