How to avoid "No such file or directory" Error for `make clean` Makefile target
Asked Answered
P

5

23

I have a Makefile that defines a .PHONY clean target for cleaning up .o files and executables, that target looks like:

...
.PHONY : clean
clean:
    rm $(addprefix $(vq_DIR),$(vq_OBJS)) \
       $(addprefix $(vq_DIR),vq) \
       $(addprefix $(covq_DIR),$(covq_OBJS)) \
       $(addprefix $(covq_DIR),covq) \
       $(addprefix $(covq_2_DIR),$(covq_2_OBJS)) \
       $(addprefix $(covq_2_DIR),covq_2) \
       $(addprefix $(covq_2_DIR),$(test_OBJS)) \
       $(addprefix $(covq_2_DIR),test)

Everything works as it should, but when some of these files do not exist, rm raises an Error (No such file or directory), and the output says that the Makefile target failed, when it clearly did what I wanted.

Is there a good way to basically tell the rm command to "remove these files if they exist, and don't complain if they don't"? I looked up the manpage for rm, and found no such flag.

Edit: I actually didn't notice the description of the -f flag in the manpage, this is the solution.

Priesthood answered 22/2, 2014 at 3:26 Comment(0)
N
36

Use rm -f (or even better $(RM), provided by built-in make rules, which can be found out using make -p) instead of rm in your cleanrule.

Nescience answered 22/2, 2014 at 3:28 Comment(2)
Thank you! I actually must have skimmed over that option. The manpage says exactly what I need, i.e. ignore nonexistent files and arguments, never promptPriesthood
Even better: use $(RM), which is automatically defined by GNU Makefile.Thimbleful
B
7

rm -f

will FORCE and not output any error

Beggs answered 22/2, 2014 at 3:29 Comment(0)
R
4

When Targets Fail

When a target is executed, it returns a status based on whether or not it was successful--if a target fails, then make will not execute any targets that depend on it. For instance, in the above example, if "clean" fails, then rebuild will not execute the "build" target. Unfortunately, this might happen if there is no core file to remove. Fortunately, this problem can be solved easily enough by including a minus sign in front of the command whose status should be ignored:

clean:
        -rm -f *.o core

~ http://www.cprogramming.com/tutorial/makefiles.html

Rosenberger answered 14/11, 2016 at 12:36 Comment(1)
It will not be ignored if you execute make clean though. It seems to me that it is only ignored if this target was a dependency to another target.Departure
A
0

Late to the party, but here's another solution that worked with our quirky build environment:

if exist *.exe rm -f *.exe

Not output free, but reduced and exits cleanly:

# make clean
        if exist *.exe rm -f *.exe

I tried a lot of alternatives that all had issues before settling on this one.

Armourer answered 12/3, 2019 at 19:44 Comment(0)
N
-1

I've given up with rm. The following command will remove files and dirs.

find . -delete

To remove only files or only dirs, there is the -type option:

# remove only files
find . -type f -delete


# remove only dirs
find . -type d -delete

Actually, I've create a little script (based on that snippet) named bomb that removes files without complaining: https://github.com/lingtalfi/bomb

Nicolanicolai answered 30/6, 2016 at 13:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.