检查 a.out 是否存在,然后使用 make clean 删除,否则尝试删除 -- Makefile

checking if a.out exists then remove with make clean otherwise do try to remove -- Makefile

我有这个简单的 makefile,在 运行 make 命令后我得到了输出 parser,我运行起来像 #: ./parser 但有时它也会生成 a.out因此,当 make 没有使用最后一个 make 命令生成 a.out 时,运行 make clean 会发出 rm: can not remove a.out, No such file or directory

的警告

所以我喜欢我的 make clean 命令还检查 a.out 是否存在然后删除,否则不要尝试删除。我怎样才能在 clean: ....

中的 makefile 中做到这一点
parser: header.h parser.c
    gcc header.h parser.c -o parser
    
clean:
    rm parser a.out 

通常的做法是强行删除:

clean:
    rm -f parser a.out 

来自 rm 联机帮助页:

-f, --force
ignore nonexistent files and arguments, never prompt