In what order prerequisites will be made by the GNU make?

Viewed 20820

Assuming we have the rule:

a: b c d e

and b, c, d and e are independent of each other.

Is the order of making b, c, d, e defined? It seems that generally they will be made in order b, c, d, e, but may it sometimes happen, that the order will be different?

7 Answers

I'll just add this for future reference. While GNU Make may not define a specific order when it comes to processing prerequisites, POSIX make requires that they be handled in the order they've been specified, namely left-to-right. Most implementations follow this rule. POSIX even gives an example where a make implementation not following this rule could break the build process of a program:

foo: y.tab.o lex.o main.o
    $(CC) $(CFLAGS) -o $@ t.tab.o lex.o main.o

where lex.o ends up using an incorrect y.tab.h. While this can be rewritten in a way that works with GNU Make, I thought I'd share this tidbit about prerequisite ordering.

If order matters, you can selectively enforce it using recursive make. For example, suppose you don't care what order b and c are made in, as long as they're both made before d and d is made before e. Then you could write your rule as:

a: b c
    $(MAKE) d
    $(MAKE) e
    # Additional steps to make a

Be aware that depending on the complexity of d and e, this approach may do Bad Things to your build times: see Recursive Make Considered Harmful (PDF) for arguments against doing it this way.

Related