Making directories in GNU Make

[article]

make.  If you don\'t you could run into the situation where the directories have not been built.

Solution 3: use a directory \'marker\' file

If you look back at the original broken solution there\'s one rather nice feature: it builds the directory needed for a specific target and only that directory.   In a more complex example (where there were many such directories to be built) it would be nice to be able to use something like that solution while avoiding the problem of constant rebuilds as the timestamp on the directory changes.

To do that you can store a special empty file, that I call a \'marker\' file in the directory and use that as the prerequisite.  Since it\'s a normal file, normal GNU Make rebuilding rules apply and its timestamp is not affected by changes in its directory.

If you add a rule to build the marker file (and to ensure that its directory exists), it\'s possible to specify a directory as a prerequisite, by specifying the marker file as a proxy for the directory and get the desired effect.

In this example the marker file is called .f

OUT = /out

.PHONY: all
all: $(OUT)/foo.o

$(OUT)/foo.o: foo.c $(OUT)/.f
    @$(COMPILE.C) -o $@ $<

$(OUT)/.f:
    mkdir -p $(dir $@)
    touch $@

Notice how the rule to build $(OUT)/.f both creates the directory, if necessary, and touches the .f file.  Since the target is a file (the .f) it can safely be used as a prerequisite in the $(OUT)/foo.o rule.

The $(OUT)/.f rule uses the GNU Make function $(dir FILE) to extract the directory portion of the target (which is the path to the .f file) and passes that directory to mkdir.

The only disadvantage here is that it\'s necessary to specify the .f files for every rule that builds a target in a directory that might need creating.

Solution 4: use an \'order-only\' prerequisite to build the directory

If you are using GNU Make 3.80 or above another solution is to use an \'order-only\' prerequisite.  An order-only prerequiste is a prerequisite of a target that will be built before the target, but whether it is rebuilt or not does not affect the rebuilding of the target.  So GNU Make will cause an order-only prerequisite to be rebuilt but it will only affect the order in which targets are handled, and not the relationship between targets that causes rebuilding.

This is exactly what we\'d like in the original broken example: make sure that the directory has been rebuilt, but if its timestamp changes don\'t rebuild the .o file. 

Order-only prerequisites are specified by prefixing them with the bar symbol | before the prerequisites (and after any normal prerequisites have been specified). 

In fact a single character change to the broken example makes it work correctly under GNU Make 3.80 and above:

OUT = /out

.PHONY: all
all: $(OUT)/foo.o

$(OUT)/foo.o: foo.c | $(OUT)/
    @$(COMPILE.C) -o $@ $<

$(OUT)/:
    mkdir -p $@

Solution 5: use pattern rules, second expansion and a marker file

In a typical Makefile (i.e. not the sort of simple example people come up with in articles like this) targets are usually built using pattern rules.  For example, the simple example would more likely be written using a pattern rule like this:

OUT = /out

.PHONY: all
all: $(OUT)/foo.o

$(OUT)/%.o: %.c
    @$(COMPILE.C) -o $@ $<

It would be nice to be able to change the pattern rule so that it builds the directory automatically using marker files.

In GNU Make 3.81 there\'s an exciting new feature called \'second expansion\' (which is enabled by specifying the .SECONDEXPANSION target in the Makefile). 

About the author

John Graham-Cumming's picture John Graham-Cumming

John Graham-Cumming is Co-Founder at Electric Cloud, Inc . Prior to joining Electric Cloud, John was a Venture Consultant with Accel Partners, VP of Internet Technology at Interwoven, Inc. (IWOV), VP of Engineering at Scriptics Corporation (acquired by Interwoven), and Chief Architect at Optimal Networks, Inc. John holds BA and MA degrees in Mathematics and Computation and a Doctorate in Computer Security from Oxford University. John is the creator of the highly acclaimed open source POPFile project. He also holds two patents in network analysis and has others pending.

AgileConnection is one of the growing communities of the TechWell network.

Featuring fresh, insightful stories, TechWell.com is the place to go for what is happening in software development and delivery.  Join the conversation now!

Upcoming Events

Sep 24
Oct 12
Oct 15
Nov 09