CMake add_custom_target;在Visual Studio中找不到32位命令

时间:2017-08-23 17:26:18

标签: visual-studio cmake windows-subsystem-for-linux

我正在使用CMake生成我的Visual Studio项目文件。我想知道我是否可以"欺骗" Visual Studio也在Windows 10上使用Windows上的Ubuntu上的Bash编译Linux,因此我创建了一个带有以下CMakeLists.txt的小型虚拟项目:

cmake_minimum_required (VERSION 3.5)
project(linux)
set(BASH_EXE C:/Windows/System32/bash.exe)
add_custom_target(Linux ALL
    COMMAND ${BASH_EXE} -c "make -C ${CMAKE_SOURCE_DIR}/build"
)

令我惊讶的是,这实际上完成了工作,并创建了一个名为Linux的项目,它运行上述命令。不幸的是,该命令因此错误而崩溃:

'C:\Windows\System32\bash.exe' is not recognized as an internal or external command, operable program or batch file.

但我可以从我的cmd提示符运行这个确切的命令,它运行正常:

C:\Windows\System32\bash.exe -c "make -C build"

为什么Visual Studio / CMake无法找到bash可执行文件?

1 个答案:

答案 0 :(得分:0)

这可能是我遇到过的最愚蠢的问题......这篇博文就是我的答案:http://www.samlogic.net/articles/sysnative-folder-64-bit-windows.htm

问题在于Windows如何处理32位与64位程序:它将System32中的64位程序和SysWOW64中的32位程序保持在一起。只有32位程序才能看到SysWOW64,只有64位程序才能看到System32。如果32位程序需要System32中的程序,则必须使用名为sysnative的伪文件夹。

结果显示Visual Studio在32位shell中启动我的CMake命令,这意味着它无法在C:/Windows/System32/

中找到程序

所以我不得不将我的CMakeLists.txt更改为:

cmake_minimum_required (VERSION 3.5)
project(linux)
set(BASH_EXE C:/Windows/sysnative/bash.exe)
add_custom_target(Linux ALL
    COMMAND ${BASH_EXE} -c "make -C ../../build"
)

不幸的是,由于CMAKE_SOURCE_DIR变量的性质,我不得不将其更改为使用相对路径而不是绝对路径(CMAKE_SOURCE_DIR扩展到Windows路径,而不是Linux路径)。