Checking if file exists before removing it in makefile

bama_programmer

I have a makefile within a C++ project (compiler: C++11). How can you check to see if a particular file exists before removing it with a makefile command?
Here is the code:

bin: charstack.h error.h
        g++ -Wall -std=c++11 main.cpp charstack.cpp error.cpp -o bin

run:
        ./bin.exe

clean:
        rm bin.exe

# This statement removes auto generated backups on my system.
cl:
        rm charstack.h~ charstack.cpp~ main.cpp~ makefile~ error.h~ error.cpp~

How would I have the makefile check to see whether the auto generated .~ backup files exist before attempting to remove them when the user passes

make cl

in the command line? The goal here is to avoid outputting these errors to the terminal upon running "make cl":

rm: cannot remove `charstack.h~': No such file or directory
rm: cannot remove `charstack.cpp~': No such file or directory
rm: cannot remove `main.cpp~': No such file or directory
rm: cannot remove `error.h~': No such file or directory
rm: cannot remove `error.cpp~': No such file or directory
make: *** [cl] Error 1
skypjack

Honestly, that's an XY problem, it is not due neither to the fact that the project is a C++ one nor that it uses the spec C++11.

Because of that, the title of the question is a bit misleading, as well as its tags.

Anyway, you can use the option -f. From the man page of rm:

ignore nonexistent files and arguments, never prompt

So, it's enough to use the following line:

 rm -f charstack.h~ charstack.cpp~ main.cpp~ makefile~ error.h~ error.cpp~

Actually, it doesn't check if those files exist, but also it doesn't complain if they don't exist.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Checking if resource file exists

From Dev

Checking if file exists in directory

From Dev

Checking if line in file exists

From Dev

Batch checking if a file exists or not

From Dev

Checking if a word exists in a file

From Dev

Checking if destination file already exists

From Dev

PHP Checking if a file exists in a directory

From Dev

Checking if a file exists in Python 2.7

From Dev

Checking if destination file already exists

From Dev

Checking if a file exists on your computer

From Dev

Checking file exists or not in android is not working

From Dev

Syntax error checking if file exists

From Dev

Getting NullPointerException checking if file exists

From Dev

Checking if a file exists from stdin <

From Dev

Checking object exists before getting property in JavaScript

From Dev

Checking if modified URI exists before redirecting

From Dev

Checking if a list of files exists before proceeding?

From Dev

Checking if a record exists before displaying in an angular form

From Dev

Checking object exists before getting property in JavaScript

From Dev

Check whether variable exists before checking value

From Dev

ios removing an observer before it exists raises exception

From Dev

Is it faster to check if a subtring exists before removing it?

From Dev

ios removing an observer before it exists raises exception

From Dev

How to check if a file exists in Makefile

From Dev

Makefile : file exists check not consistent

From Dev

Perl - Checking for file from array exists in directory

From Java

Checking if a folder exists using a .bat file

From Dev

Checking for uploaded files with file_exists

From Dev

Symfony2 - Checking if a file exists in Twig

Related Related

HotTag

Archive