本文共 1422 字,大约阅读时间需要 4 分钟。
在makefile中, -用预告诉make命令忽略当前错误, 继续执行, 我们来简单看下:
main: main.o g++ -o main main.omain.o: main.cpp g++ -o main.o -c main.cppclean: rm main *.o
执行结果为:
taoge@localhost Desktop> make cleanrm main *.otaoge@localhost Desktop> make cleanrm main *.orm: cannot remove `main': No such file or directoryrm: cannot remove `*.o': No such file or directorymake: *** [clean] Error 1
此时, 可以把makefile中的 rm main *.o替换为: -rm main *.o , 它会忽略这些错误。 当然, 在此处, 也可以用rm -f main *.o来处理。
这个命令是非常有用的, 我们来看一个例子:
makefile为:
main: main.o g++ -o main main.omain.o: main.cpp g++ -o main.o -c main.cppclean: rm main rm 1.o rm 2.o
taoge@localhost Desktop> lsmain.cpp makefiletaoge@localhost Desktop> touch main 2.otaoge@localhost Desktop> ls2.o main main.cpp makefiletaoge@localhost Desktop> make cleanrm mainrm 1.orm: cannot remove `1.o': No such file or directorymake: *** [clean] Error 1taoge@localhost Desktop> ls2.o main.cpp makefile
可见, 前面的rm 1.o失败, 后面的不会执行, 改为:
main: main.o g++ -o main main.omain.o: main.cpp g++ -o main.o -c main.cppclean: -rm main -rm 1.o -rm 2.o
taoge@localhost Desktop> lsmain.cpp makefiletaoge@localhost Desktop> touch main 2.otaoge@localhost Desktop> ls2.o main main.cpp makefiletaoge@localhost Desktop> make cleanrm mainrm 1.orm: cannot remove `1.o': No such file or directorymake: [clean] Error 1 (ignored)rm 2.otaoge@localhost Desktop> lsmain.cpp makefiletaoge@localhost Desktop>
可见, 及时rm 1.o出错, 也会被ignored, 继续往下执行。
转载地址:http://sggvi.baihongyu.com/