Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 687
jameh Avatar asked Feb 22 '14 03:02

jameh


People also ask

How do you resolve No rule to make target?

No rule to make target generally means simply that you have a compiler version that does not match the original compiler used in the project. You only need to go to the project properties and change the compiler to use the one you installed. Another reason could be that you moved the project or files to another folder.

How do I clean my makefile?

The Cleanup Rule clean: rm *.o prog3 This is an optional rule. It allows you to type 'make clean' at the command line to get rid of your object and executable files. Sometimes the compiler will link or compile files incorrectly and the only way to get a fresh start is to remove all the object and executable files.

Can makefile target be a directory?

Yes, a Makefile can have a directory as target. However, you shouldn't. When a file is added or removed from a directory, its mtime is updated. This can cause weird behaviour by causing multiple targets depending on a single directory to become implicitly rebuilt by one another.

What does %O %c mean in makefile?

The simple answer is that %.o is a target that matches any file ending in .o. "%.o: %. c" means that any file ending in .o depends on the same filename ending in . c to be present.


1 Answers

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.

like image 65
Basile Starynkevitch Avatar answered Sep 26 '22 11:09

Basile Starynkevitch