Why/when do I need a 'clean' target?
Sometimes it seems like not a new executable is built, even though I need one, but I don't understand why. For example, when I change a Makefile, but there is already an exe开发者_如何学Gocutable, and when I execute 'make' it doesn't create an updated executable.
Isn't the whole purpose of Makefiles so that I don't need to worry about that stuff anymore?
From the GNU make documentation
The recompilation must be done if the source file, or any of the header files named as dependencies, is more recent than the object file, or if the object file does not exist.
It's not changing your Makefile that triggers it.
make clean
removes all the object files that had been created in the meantime. Normally, it's no big deal to partially recompile, i.e. only to recompile the files you changed and finally link the newly created object files with the pre-existing ones. Still, if you want to be absolutely safe, you should run make clean
before running make
again.
An example where keeping old object files (i.e. never running make clean) may become a problem: Suppose you have an already existing object file that is meant to be linked against version 1.0 of some library. Now you update your machine and this will install version 1.1 on it, where some function is incompatible to the of function 1.0. But since your object file was compiled expecting the prior version the linking process will ultimately fail.
Well, it's a very broad question. Generally, you can write a makefile which will track all the dependencies (including it's own modification time). However it's non trivial and bugs can crawl to your makefile the same as to any other code. So, sometimes it's easier to clean everything and rebuild when suspecting that something was not built correctly.
There are many other build tools, such as scons which might be more robust/automatic than makefile.
Make analyzies the depedencies defined in your Makefiles and build a dependency graph out of that.
If it detects that any of the prequisites need by the output (binaries) changes it is rebuild - or at least the part that has changed.
So, if your dependency includes the Makefiles - it probably shouldn't - make would update the binary once you change the Makefile.
Life with Makefiles doesn't always easier but it can help you very much anyway.
精彩评论