GNU make: order-only prerequisites

Boris Kolpackov boris at
Thu Jan 22 13:21:27 CST 2004

A new flavor of prerequisites was introduced in GNU make 3.80 called
order-only prerequisites. They are essentially the same as normal ones
except that an out-of-date order-only prerequisite does not render its 
targets out-of-date. I found this especially useful in the following 
context. Suppose I need to build 'foo.o' from 'foo.c' in $(out_dir).
In the GNU make syntax such a desire can be expressed, for example, 
like this:

$(out_dir)/foo.o : foo.c
	g++ -c $^ -o $@

So far so good. Now, suppose the directory $(out_dir) does not always
exist and I need to create it before producing 'foo.o'. The first thing 
that comes to mind is to write something along these lines:

$(out_dir) :
	mkdir -p $@ 

$(out_dir)/foo.o : $(out_dir)

This doesn't work because now we have two prerequisites for 
'$(out_dir)/foo.o' and when the rule's command is executed, we get 
something like this (assuming $(out_dir) is /bar):

gcc -c foo.c /bar -o /bar/foo.o

Of course, we could filter out all prerequisites that don't have suffix
'.c' but that wouldn't be a very elegant solution. A much cleaner approach 
would be to use an order-only prerequisite:

$(out_dir)/foo.o : | $(out_dir)

This will ensure that if $(out_dir) does not exist then it will be created
before '$(out_dir)/foo.o' is built.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 652 bytes
Desc: Digital signature
Url :

More information about the notes mailing list