Makefile clean not removing *.o files?

39,025

Solution 1

To check what really happens, run "make clean" and examine the output of that command.

  1. Is it nothing? Then there might be a file called "clean" in the current directory. Remove it and try again.
  2. Does it start with "rm ..."? Then it seems to be normal.
  3. In all other cases, tell us the exact output you get.

To check whether the commands are really run, insert some "echo" commands before and after the "rm" command. Are they executed?

And finally, did you distinguish between tab characters and spaces? In Makefiles the difference is important. Commands must be indented using tabs.

Solution 2

One way that make clean can 'fail' to execute anything is if there is a file called clean in your directory, possibly the result of running make -t clean. This would create a file, so when you next ran make clean, it would appear up to date - it has no dependencies, so there is no reason to run the action.

If you use GNU Make, ensure that you have the line:

.PHONY: clean

That will stop make -t from creating clean and will ensure that the actions are run.

Share:
39,025

Related videos on Youtube

nacho4d
Author by

nacho4d

C/C++/Objective-C/C#/Javascript/shell-script/Japanese/Go/Spanish/English I love this site. So Helpful! https://twitter.com/nacho4d http://nacho4d-nacho4d.blogspot.com/

Updated on December 27, 2020

Comments

  • nacho4d
    nacho4d almost 2 years

    I wonder why this won't delete/clean *.o files generated when running make?

    # UNIX Makefile
    CXX = g++
    LD  = g++
    CXXFLAGS = -g
    testlfunction: lfunction.o lfunctionlist.o lprocessor.o testlfunction.o
        $(LD) -o [email protected] $^
    clean:
        rm *.o testlfunction
    

    before it use to be

    $(RM) *.o testlfunction
    

    but it didn't work also ;(

    Why is this?

    • Jonathan Leffler
      Jonathan Leffler almost 12 years
      The difference between rm and $(RM) is likely to be that RM = rm -f which won't fail if one of the listed files is missing, whereas using just rm means that if any of the listed files is missing, the clean operation will fail. For example, if you do make clean all but the clean fails, the all phase won't be built.

Related