makefile中“-“符号的使用

       在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, 继续往下执行。

 

 


 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值