"Generally, stick to the widely-supported (usually posix-specified) options and features of these programs. For example, don't use ‘mkdir -p’, convenient as it may be, because a few systems don't support it at all and with others, it is not safe for parallel execution. " gnu.org/s/hello/manual/make/Utilities-in-Makefiles.html
–
greg.kindelDec 19 '11 at 17:28

2

-p doesn't work on Windows, which is presumably what the question is asking about. Not sure how this ever got accepted.
–
AntimonyMay 6 '14 at 2:00

You should see here the usage of the | pipe operator, defining an order only prerequisite.
Meaning that the $(OBJDIR) target should be existent (instead of more recent) in order to build the current target.

Note that I used mkdir -p. The -p flag was added compared to the example of the docs.
See other answers for another alternative.

This is definitely the official way to this problem.
–
lcltjNov 12 '11 at 20:50

@CraigMcQueen: I truly meant "the $(OBJDIR) target should be existent". Make checks for file presence (and timestamps) to decide if it need to build the target. Hence here, if $(OBJDIR) is absent it will build it, so that it is existent in order to build the current target $(OBJS), that will be created inside.
–
TeKaJul 3 '12 at 14:37

This is the preferred way if you need your makefiles to work in both Windows (with gnuwin32 and PowerShell) and POSIX-compliant OS's.
–
Kris HardyJan 6 '12 at 18:56

5

PROVIDED you have the gnuwin32 CoreUtils package to provide the 'test' utility.
–
davenpcjAug 2 '13 at 1:36

Way late here, but I'd like to point out that this solution can break when building in parallel (make -j) due to a race condition between when the directory is tested for existence and it is created. One job can test that it's not there but before it creates it, another job creates the directory. Then when the first tries to make it, it will fail because the directory already exists. The best solution in this case is to use order only prerequisites as mentioned in @TeKa's answer (which should be the accepted answer).
–
C0deH4ckerJul 10 '14 at 17:30

Here is a trick I use with GNU make for creating compiler-output directories. First define this rule:

%/.d:
mkdir -p $(@D)
touch $@

Then make all files that go into the directory dependent on the .d file in that directory:

obj/%.o: %.c obj/.d
$(CC) $(CFLAGS) -c -o $@ $<

Note use of $< instead of $^.

Finally prevent the .d files from being removed automatically:

.PRECIOUS: %/.d

Skipping the .d file, and depending directly on the directory, will not work, as the directory modification time is updated every time a file is written in that directory, which would force rebuild at every invocation of make.

Ah, thanks, I was trying to get something like this to work. I don't want "test -d foo || mkdir foo" on several goals, since then using "make -j2" will test them at the same time, both tests giving false, and then two processes will try to mkdir, the last of them failing.
–
unhammerSep 19 '10 at 18:32

If you want to see another little generic gmake trick, or if you're interested in non-recursive make with minimal scaffolding, you might care to check out Two more cheap gmake tricks and the other make-related posts in that blog.