Skip to content
Advertisement

Using gnu make with a makefile, why is my .o file not deleted?



I have a simple makefile that works correctly with gnu make, except the object file named ‘txt.o’ is not removed, using ‘rm tst.o’. Here is the text from the makefile.

JavaScript

Anyone know why the rm command here is not removing the ‘tst.o’ file?

Thanks,
Matt

Advertisement

Answer

What command did you enter for the make?

If you just enter make, it will choose the first target it finds. In this case, that target is tst:

JavaScript

The make will proceed according to the dependencies, so it will also make the target tst.o if tst is not up to date with it.

Your removal is the target clean which, the way your makefile is structured, won’t be executed unless you enter:

JavaScript

If you want it to clean up after the build, include it in your target. One way you can do this is by creating a new target that makes and cleans. Make this your first target:

JavaScript

Now, make or make build_all will build the tst target then do clean.

However, as was pointed out in the comments, there are good reasons to keep your .o file around (e.g., not causing it to be rebuilt every time when not necessary).

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement