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

后端 未结 5 2000
野的像风
野的像风 2021-01-04 00:28

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 $(add         


        
相关标签:
5条回答
  • 2021-01-04 00:53

    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.

    0 讨论(0)
  • 2021-01-04 00:56

    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

    0 讨论(0)
  • 2021-01-04 00:58

    rm -f

    will FORCE and not output any error

    0 讨论(0)
  • 2021-01-04 00:59

    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.

    0 讨论(0)
  • 2021-01-04 01:03

    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

    0 讨论(0)
提交回复
热议问题