0
targetOne : header/headerAB.h source/sourceA.cpp source/sourceB.cpp source/main.cpp
         g++ -Wall header/headerAB.h source/sourceA.cpp source/sourceB.cpp source/main.cpp

Is there a reason why I should break this in multiple targets rather then keeping it as one?
In what cases do we need to have separate targets rather than one in a Makefile?

4

2 回答 2

1

Citing http://mrbook.org/tutorials/make/

Using dependencies. Sometimes is useful to use different targets. This is because if you modify a single file in your project, you don't have to recompile everything, only what you modified.

So you will get faster recompilation after partial source update. And this is "must to implement" for huge projects, where any change is very small (comparing to entire > 1M LOC project).

Also, multiple targets allows you to set different rules for different parts of project.

于 2012-07-02T12:14:16.180 回答
1

Generally, for flexibility and scalability you should ask 'what is the advantage of building multiple files in the same rule'. It is probable that the first time you do a build, it's going to be quicker if everything gets compiled with the same rule.

However on subsequent steps, if you just alter one source code file do you really want all the other source code files built to object files? I'd imagine not. But if you have it all done in the one rule, that's what'll happen. And that will be expensive if you have a lot of source code files.

This also means if you're building libraries, you'll want to build the library as a separate step, rather than saying having one rule that compiles all the sources and then archives the library. Of course, in that circumstance, it means that the library archive step doesn't need to know how the source code was compiled, or even what language it was in, which makes for easier maintenance.

于 2012-07-02T12:24:16.257 回答