src/Makefile
SHELL := cmd.exe
target:
setlocal EnableDelayedExpansion
set foo=hello
echo !foo! %foo%
运行目标
cd src
mingw32-make.exe
// output: !foo!
为什么
setlocal EnableDelayedExpansion
在 Makefile 中不起作用?
mingw32-make.exe --version
GNU Make 3.82.90
Built for i686-pc-mingw32
配方中的每个操作都在单独的 shell 中执行。这意味着,您的配方相当于 shell 脚本:
bash -c setlocal EnableDelayedExpansion
bash -c set foo=hello
bash -c echo !foo! %foo%
有两种方法可以在一次 shell 调用中执行配方。
首先,你可以写一个多行:
target:
setlocal EnableDelayedExpansion \
set foo=hello \
echo !foo! %foo%
.ONESHELL
那么你的makefile
可以是
.ONESHELL:
target:
setlocal EnableDelayedExpansion
set foo=hello
echo !foo! %foo%
这两种方法都有文档中描述的局限性和警告。选择最适合您和您的情况的方法。