Aquarius_Girl
Aquarius_Girl

Reputation: 22906

order of dependencies in make

Since it is said that order of dependencies does matter in a makefile. I wish to understand how:

finalObjectFile: x.o main.o y.o
    g++ x.o main.o y.o -o finalObjectFile

main.o: header/x.h source/main.cpp
    g++ -c source/main.cpp

x.o: header/x.h source/x.cpp
    g++ -c source/x.cpp

y.o: source/y.cpp header/x.h
    g++ -c source/y.cpp

In the above code I have swapped the positions of the header file x.h:

x.o: header/x.h source/x.cpp
        g++ -c source/x.cpp

y.o: source/y.cpp header/x.h
        g++ -c source/y.cpp

but the output doesn't get effected!

Which kind of dependencies are ACTUALLY important to be in order?

Upvotes: 1

Views: 3797

Answers (2)

James Kanze
James Kanze

Reputation: 153899

It matters in real make files, because you tend to use packaged rules. In a rule, $< is replaced by the first prerequisite. Thus:

compile = g++ -c $< -o $@

x.o : source/x.cpp header/x.h
    $(compile)

y.o : header/y.h source/y.cpp
    $(compile)

The first rule will compile source/x.cpp, as desired. The second rule will attempt to compile header/y.h, which is definitely not what is wanted.

Since this is a far more typical (and rational) way of writing makefiles, you do have to respect order, at least with regards to which element comes first.

Another case it might matter (but which shouldn't be the case in a well written makefile): in many cases, make will resolve the dependencies in a left to right order; i.e. given:

x.o : source/x.cpp header/x1.h header/x2.h
    ...

make will first check that source/x.cpp is up to date, and build it if necessary, and then do the same for header/x1.h and header/x2.h, in that order. If you have any non-stated dependencies, which require header/x1.h to be built before header/x2.h, then changing their order will also cause problems. This is, however, an error in the makefile; the dependency should be made explicit (e.g. a rule with header/x1.h : header/x2.h). Because this order is not guaranteed otherwise; in the case of GNU make, for example, it may be violated if you use the -j option. header/x.h, then swapping the order may cause problems.

Upvotes: 2

Jonathan Leffler
Jonathan Leffler

Reputation: 753505

In the context shown, there is no significant difference between header before source and source before header. The rules say (for example):

  • x.o must be more recent than header/x.h and source/x.cpp.

It doesn't matter which sequence those two dependencies appear in; if either dependency is newer than x.o, the command will be executed.

When does the order matter? That's a bit trickier to exemplify, but might be a factor if you have multiple intermediate files that get generated as you go. It might especially be a problem if some of the commands you execute affect multiple files but you don't properly tell make about all those interactions.

However, dependency order normally isn't a problem.

Upvotes: 3

Related Questions