远程系统没有 CMake 3.8 或更高版本

The remote system does not have CMake 3.8 or greater

  1. 问题介绍

我正在尝试使用 Windows 上的 Visual Studio 2022(最新版本 17.2.0)和 CMake 模板创建一个在 C++ 中打印“Hello World”的 MacOS 应用程序,这样我就可以远程连接(使用 SSH)到 MacOS,我一直在关注 this official Microsoft tutorial

  1. 出现问题

问题是,当我进入 MacOS 中的 CMake 安装步骤时,我无法识别安装的 MacOS 版本,因为当我在 Windows 中打开项目时,我收到以下消息在控制台中:

1> Copying files to the remote machine.
1> Starting copying files to remote machine.
1> Finished copying files (elapsed time 00h:00m:00s:650ms).
1> CMake generation started for configuration: 'macos-debug'.
1> Found cmake executable at /Users/maria/.vs/cmake/bin/cmake.
1> The remote system does not have CMake 3.8 or greater. An infobar to automatically deploy CMake to the remote system will be displayed if you are building on a supported architecture. See https://aka.ms/linuxcmakeconfig for more info.

上面还显示了以下消息:

Supported CMake version is not present on 'remote address'. Install  latest CMake binaries from CMake.org? Yes No

当我按“是”时,它说我实际上已经在远程 MacOS 上安装了 cmake:

1> Copying files to the remote machine.
1> Starting copying files to remote machine.
1> Finished copying files (elapsed time 00h:00m:00s:650ms).
1> CMake generation started for configuration: 'macos-debug'.
1> Found cmake executable at /Users/maria/.vs/cmake/bin/cmake.
1> The remote system does not have CMake 3.8 or greater. An infobar to automatically deploy CMake to the remote system will be displayed if you are building on a supported architecture. See https://aka.ms/linuxcmakeconfig for more info.
CMake binary deployment to the remote machine started. CMake generation will continue automatically after deployment finishes.

CMake binary deployment to the remote machine failed: Installation directory '/Users/maria/.vs/cmake' already exists.
  1. 解决方案尝试

我尝试使用 brew(最新版本 3.23.1)安装 CMake,并确保可以直接从 MacOS 终端访问 cmake(包含在 PATH 中),我还尝试按照 the official guide 通过将“CMake.app”复制到“/Applications”并使用以下命令将其添加到路径来安装图像 .dmg:

export PATH=/Applications/CMake.app/Contents/bin:$PATH

我什至尝试安装旧版本的 CMake(如 3.8.0 或 3.8.1),但同样的事情仍然发生。预期结果与此处显示的 Microsoft 指南相同:

1> Copying files to the remote machine.
1> Starting copying files to remote machine.
1> Finished copying files (elapsed time 00h:00m:00s:650ms).
1> CMake generation started for configuration: 'macos-debug'.
1> Found cmake executable at /Applications/CMake.app/Contents/bin/cmake.
1> /Applications/CMake.app/Contents/bin/cmake -G "Ninja"  DCMAKE_BUILD_TYPE_STRING="Debug" -DCMAKE_INSTALL_PREFIX
1> [CMake] -- Configuring done
1> [CMake] -- Generating done
1> [CMake] -- Build files have been written to: /Users/cti/.vs/CMakeProject90/out/build/macos-debug
1> Extracted CMake variables.
1> Extracted source files and headers.
1> Extracted code model.
1> Extracted includes paths.
1> CMake generation finished.

有谁知道为什么会这样或者有什么办法可以解决这个问题?

我终于解决了这个问题,我不得不将位于 CMake 应用程序中的文件复制:/Applications/CMake.app/Contents/bin/cmake 到 Visual Studio 试图找到它们的位置,在我的例子中是: /Users/maria/.vs/cmake/bin/cmake

版本不是问题,因为我使用最新的 CMake 版本 (3.23.1) 进行了尝试,并且可以正常工作。最后,我发现了一个问题,该问题与缺少指示 C++ 和 C 的编译器位置以及 Ninja 的位置有关,我只是在 CMakePresets.json 中指定它并且我没有遇到重大问题:

    {
        "name": "macos-debug",
        "displayName": "macOS Debug",
        "generator": "Ninja",
        "binaryDir": "${sourceDir}/out/build/${presetName}",
        "installDir": "${sourceDir}/out/install/${presetName}",
        "cacheVariables": {
          "CMAKE_BUILD_TYPE": "Debug",
          "CMAKE_C_COMPILER": "/usr/bin/gcc",
          "CMAKE_CXX_COMPILER": "/usr/bin/g++",
          "CMAKE_MAKE_PROGRAM": "/usr/local/bin/ninja"
        },
        "condition": {
            "type": "equals",
            "lhs": "${hostSystemName}",
            "rhs": "Darwin"
        },
        "vendor": {
            "microsoft.com/VisualStudioRemoteSettings/CMake/1.0": {
                "sourceDir": "$env{HOME}/.vs/$ms{projectDirName}"
            }
        }
    }

我希望有人在使用 Visual Studio(在 Windows)

的 MacOS 上进行开发时会发现这是一个有用的解决方案

这似乎是一个 Visual Studio 错误。你可以跟踪它 here.

解决方法

看起来 Visual Studio 总是在当前通过 SSH 用户(即 ~/.vs/cmake/bin/cmake)连接的本地文件夹下寻找 CMake,无论您如何安装它。然后,当 Visual Studio 建议安装它时:

Supported CMake version is not present on ‘192.168.1.180’. Install latest CMake binaries from CMake.org?

如果您同意这样做,它实际上会在上述文件夹中本地推出。如果您尝试在本地 Mac 机器上使用它,二进制文件 Visual Studio 使用已损坏并抛出错误:

$: ~/.vs/cmake/bin/cmake
zsh: exec format error: /Users/User/.vs/cmake/bin/cmake

这就是为什么 Visual Studio 一直在努力寻找可用的 CMake 二进制文件。您可以通过使用工作 CMake 二进制文件代替文件夹 Visual Studio 为文件夹创建一个符号 link 来解决这个问题 在以下位置查找它们:

$: rm -rf ~/.vs/cmake/bin
$: ln -s /Applications/CMake.app/Contents/bin ~/.vs/cmake/bin

此时Visual Studio将能够找到CMake,但无法找到默认的编译器和生成器:

1> /Users/User/.vs/cmake/bin/cmake -G "Ninja"   -DCMAKE_BUILD_TYPE:STRING="Debug" -DCMAKE_INSTALL_PREFIX:PATH="/Users/User/.vs/CrossPlatform/out/install/macos-debug"  /Users/User/.vs/CrossPlatform/CMakeLists.txt;
1> [CMake] CMake Error: CMake was unable to find a build program corresponding to "Ninja".  CMAKE_MAKE_PROGRAM is not set.  You probably need to select a different build tool.
1> [CMake] CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
1> [CMake] CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage

最简单的修复方法就是在 Mac 机器上使用 运行 这个命令。它将生成缓存 Visual Studio 然后可以使用,但是请注意,无论何时此缓存因任何原因(例如,在配置之间切换时)无效,您都必须以相同的方式再次 re-generate 它。另一种选择是明确指定所有缺少的参数(通过 CMakeLists.txt 或作为 cacheVariables 属性 下的命令行参数CMakePresets.json)