7

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 $@ $^

clean:
    rm *.o testlfunction

before it use to be

$(RM) *.o testlfunction

but it didn't work also ;(

Why is this?

nacho4d
  • 43,720
  • 45
  • 157
  • 240
  • no, everything normal... what is the difference between rm and $(RM) ? – nacho4d Dec 26 '10 at 22:29
  • 3
    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. – Jonathan Leffler Dec 26 '10 at 23:37
  • "It didn't work" is never an appropriate error description. Do you remember the exact error messages, or any other output? – Roland Illig Feb 05 '21 at 21:33

2 Answers2

9

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.

Roland Illig
  • 40,703
  • 10
  • 88
  • 121
6

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278