在makefile中, -用预告诉make命令忽略当前错误, 继续执行, 我们来简单看下:
main: main.o
g++ -o main main.o
main.o: main.cpp
g++ -o main.o -c main.cpp
clean:
rm main *.o
执行结果为:
taoge@localhost Desktop> make clean
rm main *.o
taoge@localhost Desktop> make clean
rm main *.o
rm: cannot remove `main': No such file or directory
rm: cannot remove `*.o': No such file or directory
make: *** [clean] Error 1
此时, 可以把makefile中的 rm main *.o替换为: -rm main *.o , 它会忽略这些错误。 当然, 在此处, 也可以用rm -f main *.o来处理。
这个命令是非常有用的, 我们来看一个例子:
makefile为:
main: main.o
g++ -o main main.o
main.o: main.cpp
g++ -o main.o -c main.cpp
clean:
rm main
rm 1.o
rm 2.o
taoge@localhost Desktop> ls
main.cpp makefile
taoge@localhost Desktop> touch main 2.o
taoge@localhost Desktop> ls
2.o main main.cpp makefile
taoge@localhost Desktop> make clean
rm main
rm 1.o
rm: cannot remove `1.o': No such file or directory
make: *** [clean] Error 1
taoge@localhost Desktop> ls
2.o main.cpp makefile
可见, 前面的rm 1.o失败, 后面的不会执行, 改为:
main: main.o
g++ -o main main.o
main.o: main.cpp
g++ -o main.o -c main.cpp
clean:
-rm main
-rm 1.o
-rm 2.o
taoge@localhost Desktop> ls
main.cpp makefile
taoge@localhost Desktop> touch main 2.o
taoge@localhost Desktop> ls
2.o main main.cpp makefile
taoge@localhost Desktop> make clean
rm main
rm 1.o
rm: cannot remove `1.o': No such file or directory
make: [clean] Error 1 (ignored)
rm 2.o
taoge@localhost Desktop> ls
main.cpp makefile
taoge@localhost Desktop>
可见, 及时rm 1.o出错, 也会被ignored, 继续往下执行。