Makefile Windows: .. 更改目录时未被识别为内部或外部命令

Makefile Windows: .. is not recognized as an internal or external command when changing directory

我在尝试从 Windows 中的 Makefile 内部更改目录时遇到了一个奇怪的问题。我的伪代码如下:-

all:
    cd ../ProjectDir && ../AutoExc InputFile.h

其中 AutoExc 是我在父目录中的可执行文件。当我从命令行 运行 'make' 时,我看到以下输出:-

cd ../ProjectDir && ../AutoExc InputFile.h
'..' is not recognized as an internal or external command.

奇怪的是 运行 在 Linux 中使用这个确切的 Makefile。我也在 Windows 的命令行中尝试 运行ning 上面的命令并且它有效,所以 'cd' 没有问题。

知道为什么会这样吗?我该怎么做才能让它发挥作用?如果 Make 的版本有问题,是否有另一种更可靠的机制来更改目录和 运行 Windows 上的可执行文件?

我在 Windows 7(最新)上使用 Make 版本 3.81,在 Linux 4.15.0-20-generic #21-Ubuntu 上使用 Make 版本 4.1。我安装了 'cd' 作为 'Git for Windows' 的一部分,git 版本是 2.16.1.windows.4.

您的命令包含 shell 个字符并传递给 cmd.exe:

>cd ../Public &&  ../test
'..' is not recognized as an internal or external command,
operable program or batch file.

>cd ../Public &&  ..\test
'..\test' is not recognized as an internal or external command,
operable program or batch file.

cmd.exe../test 解释为带有选项 /test 的命令 ..

我想一种解决方案是将宏应用于带路径的命令名称,例如(注意:未经测试,只是从我的脑子里打出来的):

if (...I'm running under Windows...)
  convert_path = $(subst /,\,$(1))
else
  convert_path = $(1)
endif

all:
    cd ../ProjectDir && $(call convert_path,../AutoExc) InputFile.h

或者,如果可能,使用评论 SHELL := /bin/sh 中的建议,或者在您的 Windows 环境中使用与 UNIX 兼容的 shell 的任何正确路径。 "Git for Windows" 是基于 MinGW 的 AFAIR,所以你应该有 bash 可用。