如何通过 CMAKE 中的 COMMAND 执行路径中有空格的 .bat 文件

How do I execute a .bat file through COMMAND in CMAKE that has spaces in it's path

我正在尝试通过 execute_process 宏通过 CMake 脚本执行一个简单的 .bat 文件。到目前为止,我已经尝试了这三个:

execute_process(COMMAND cmd /c "E:/dev/test folder with spaces/test with spaces.bat")
execute_process(COMMAND cmd /c "\"E:/dev/test folder with spaces/test with spaces.bat\"")
execute_process(COMMAND cmd /c [=["E:/dev/test folder with spaces/test with spaces.bat"]=])

None 其中有效,输出如下所示:

'E:/dev/test' is not recognized as an internal or external command,
operable program or batch file.
'\"E:/dev/test folder with spaces/test with spaces.bat\"' is not recognized as an internal or external command,
operable program or batch file.
'\"E:/dev/test folder with spaces/test with spaces.bat\"' is not recognized as an internal or external command,
operable program or batch file.

我做错了什么?

我不知道如何重现您的问题。我在 D: 驱动器中重新创建了您的示例(我没有 E:)。

D:\>tree /F "test folder with spaces"
Folder PATH listing for volume Sources
Volume serial number is 000000DD 943F:828D
D:\TEST FOLDER WITH SPACES
    test with spaces.bat

No subfolders exist

D:\test folder with spaces\test with spaces.bat中,我放了下面的小脚本:

@echo off
echo Success!

然后我在D:\test.cmake中创建了一个测试脚本test.cmake:

cmake_minimum_required(VERSION 3.20)

execute_process(
  COMMAND cmd /C "D:/test folder with spaces/test with spaces.bat"
)

我 运行 看到了这个:

D:\>cmake -P test.cmake
Success!

我只能推断您使用的是非常旧的 CMake 版本,您应该升级。