首页 > 解决方案 > 为什么不能从 cmake 脚本调用 wsl?

问题描述

我有生成 c++ 文件的 bash 脚本,我想从 cmake 中的 add_custom_command() 调用它们。在windows上,我想通过wsl(windows subsytem for linux)调用bash脚本。cmake 配置良好(找到 wsl.exe),但在构建时它抱怨:

'C:\Windows\System32\wsl.exe' is not recognized as an internal or external command,

这是一个要重新创建的简单 cmake 脚本。它在 linux 环境(包括 wsl)中成功,但在本机 windows 环境中失败。

cmake_minimum_required(VERSION 3.7)
project(WhyNotWSL)

set(source ${CMAKE_SOURCE_DIR}/Input/main.cpp)
set(target  generated.cpp )

if (WIN32)
    find_program(WSL wsl)
    message("WSL is ${WSL}")
  set (command ${WSL} cp ${source} ${target})
else()
  set (command cp ${source} ${target})
endif()

message("command is ${command}")

add_custom_target( ${target} )

add_custom_command(
    OUTPUT ${target}
    COMMAND ${command}
    DEPENDS ${source}
    COMMENT "Generated ${target}"
    )

add_custom_target(p ALL
    DEPENDS ${target}
    )

add_executable(hello ${target})

以下输出来自 cmake 配置:

-- Building for: Visual Studio 16 2019
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.18363.
-- The C compiler identification is MSVC 19.27.29110.0
-- The CXX compiler identification is MSVC 19.27.29110.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.27.29110/
bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.27.2911
0/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
**WSL is C:/Windows/System32/wsl.exe**
**command is C:/Windows/System32/wsl.exe;cp;D:/Development/CMakeWSLTest/Input/main.cpp;generated.cpp**
-- Configuring done
-- Generating done
-- Build files have been written to: D:/Development/CMakeWSLTest/BUILD

以下是 cmake build 的输出

Microsoft (R) Build Engine version 16.7.0+b89cb5fde for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

  Checking Build System
  Building Custom Rule D:/Development/CMakeWSLTest/CMakeLists.txt
  Building Custom Rule D:/Development/CMakeWSLTest/CMakeLists.txt
  Generated generated.cpp
  'C:\Windows\System32\wsl.exe' is not recognized as an internal or external command,
  operable program or batch file.

作为记录,以下从 DOS shell 工作:

>C:\Windows\System32\wsl.exe cp ../Input/main.cpp generated.cpp

标签: cmakewindows-subsystem-for-linux

解决方案


尝试给出提示find_program()并指向C:\Windows\Sysnative

像这样的东西:

if (WIN32)
    find_program(WSL wsl HINTS C:/Windows/Sysnative)
    message("WSL is ${WSL}")
  set (command ${WSL} cp ${source} ${target})
else()
  set (command cp ${source} ${target})
endif()

这里的理由:https ://superuser.com/a/1528297/2201但基本上,这可能是由您的 cmake 构建引起的。find_program()找到具有绝对路径的可执行文件,然后在构建期间实际调用它时应该不同,它实际上应该从另一个位置指向二进制文件,因为继承了构建的父进程的构建类型。

也许..


推荐阅读