在 Makefile 中设置 local EnableDelayedExpansion (Microsoft Windows)

问题描述 投票:0回答:1

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
makefile cmd dos
1个回答
0
投票

配方中的每个操作都在单独的 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%

在一个 shell 中执行配方的另一种方法是使用特殊目标

.ONESHELL
那么你的
makefile
可以是

.ONESHELL:
target:
    setlocal EnableDelayedExpansion
    set foo=hello
    echo !foo! %foo%

这两种方法都有文档中描述的局限性和警告。选择最适合您和您的情况的方法。

© www.soinside.com 2019 - 2024. All rights reserved.