Remove or Keep intermediate files using Makefile
Make is a very complex tool, but if we ignore most of its dark corners, it could be handy.
1 | // main.c |
This is the common Makefile one would write for the above program.
1 | # Makefile |
Then, running make
to create the final executable, and make clean
to remove all generated files work OK.
However, after running make
(without make clean
), one may notice there’s this main.o
(intermediate file) lying
around in the current directory. If you find it annoying, the below Makefile could be used to remove intermediate files
automatically.
1 | # Makefile |
The reason for such different behavior is that main.o
is mentioned in the first Makefile, namely main.out: main.o
.
Therefore, it’s not considered as an intermediate file, but a target instead. Running make
alone would auto remove
intermediate files, but not targets; after all, make is supposed to build targets.
See Chains of Implicit Rules for more info.