Prevent GNU make from always removing files. It says things like “rm …” or “Removing intermediate files…”.

By neokrates, written on August 18, 2012


  • Join date: 11-30-99
  • Posts: 224
View Counter:
Rate it
  • You parse your logs using?

    View Results

    Loading ... Loading ...
  • bodytext bodytext bodytext

After the make build some files are missing. There is no rule in your make files which tells Make to remove the files. In the build logfile you find lines like “rm …” and “Removing intermediate files…”. These are often last entries in the log, after the last target is finished (Successfully remade target file `all’.). Why make keeps removing the files? How to preserve them?

Woks for:

✔ windows xp or Linux

✔ GNU Make 3.*

Should also work for:

✔ most current GNU Make versions

✔ Os linke windows, Linux or Mac OS

In short :
Gnu make tracks some files which it calls "intermediate" and will remove them in the end of the build. You can verify that that is the reason your files are missing using make --debug and then you can preserve the files using special .SECONDARY: make target.


Run your build again, use --debug option

So, can this howto solve your problem? Let us check … Open dos shell in the your make directory, do

> make  --debug

Does the Make output contain (likely in the end) something like ? :

Successfully remade target file `$(smth like all)'.
Removing intermediate files...

If yes, this howto is for you. If no, your problem is something else.


Add .SECONDARY: line in your make file

Go ahead and add .SECONDARY: line into make file, like

... your code goes here


Run make again

> make  --debug

verify that Removing intermediate files... entry has disappeared and that your files are no longer being removed .


Refine, optimize the solution

Mind that we just forced the make to preserve all intermediate files by adding .SECONDARY: line. Now you might get many temporary files cerated by make executions and just lying around. No cleanup.

You normally would not want that.

To improve the dirty fix, you may try to specify which files should be preserved by naming the make targets which .SECONDARY applies to

.SECONDARY: preserveme1.src preserveme2.src

(I didn’t try that out, but manual says that should work)


Why files are being removed, what is the problem, simple explanation and links to learn more

GNU make does track some files, which are created during the build, and it will remove such files after the build.
The files are called "intermediate files" . They are supposed to be created by the make’s "chain of Implicit Rules" . Because they where created to facilitate something else, makes considers them useless after the build and removes them. :
“… if make does create b in order to update something else, it deletes b later on after it is no longer needed. ”

So, what does the
“.SECONDARY with no prerequisites causes all targets to be treated as secondary (i.e., no target is removed because it is considered intermediate). ”

Here is also a nice discussion on that subject :

hope I could help you, have fun :)

Be Sociable, Share!
Does that help to solve your problem?
VN:F [1.8.5_1061]
Rating: +11 (from 11 votes)
11 votes 'YES'  0 votes 'NO'

LEARN MORE (amazon bookstore)


Be Sociable, Share!


Leave a Reply