我想在makefile中包含一些条件语句:
SHELL=/bin/bash
all:
$(g++ -Wall main.cpp othersrc.cpp -o hello)
@if [[ $? -ne -1 ]]; then
echo "Compile failed!";
exit 1;
fi
但是得到一个错误:
/bin/bash:-c:第0行:需要条件二进制运算符/bin/bash:-c: 第0行:如果[-ne-1]],
-1' /bin/bash: -c: line 0:
附近出现语法错误;然后\'makefile:3:目标'all'的配方失败make:***[all]错误1
如何修复?
请注意,makefile配方的每一行都在不同的shell中运行,因此前一行的$?
不可用,除非使用.ONESHELL
选项。
没有.ONESHELL
:的修复
all: hello
.PHONY: all
hello: main.cpp othersrc.cpp
g++ -o $@ -Wall main.cpp othersrc.cpp && echo "Compile succeeded." || (echo "Compile failed!"; false)
带.ONESHELL
:
all: hello
.PHONY: all
SHELL:=/bin/bash
.ONESHELL:
hello:
@echo "g++ -o $@ -Wall main.cpp othersrc.cpp"
g++ -o $@ -Wall main.cpp othersrc.cpp
if [[ $$? -eq 0 ]]; then
echo "Compile succeded!"
else
echo "Compile failed!"
exit 1
fi
当$
需要传递到shell命令中时,它必须在makefile中被引用为$$
(基本上,make
传递一美元会收取一美元的费用(。因此CCD_ 9。