- 問題簡介
我正在嘗試創建一個 MacOS 應用程式,該應用程式使用 Windows 上的 Visual Studio 2022(最新版本 17.2.0)和 CMake 模板在 C 中列印“Hello World”,這樣我就可以遠程(使用 SSH)連接到 MacOS,我一直在關注這個微軟官方教程
- 出現問題
問題是,當我在 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.
- 解決方案嘗試
我嘗試使用 brew 安裝 CMake(可用的最新版本 3.23.1)并確保可以直接從 MacOS 終端訪問 cmake(包含在 PATH 中),我還嘗試按照官方指南安裝影像 .dmg通過將“CMake.app”復制到“/Applications”并使用以下命令將其添加到路徑中:
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.
有誰知道為什么會發生這種情況或有什么辦法可以解決這個問題?
uj5u.com熱心網友回復:
這似乎是一個 Visual Studio 錯誤。你可以在這里跟蹤它。
解決方法
看起來 Visual Studio 總是在當前通過 SSH 用戶(即~/.vs/cmake/bin/cmake)連接的本地檔案夾下查找 CMake,無論您如何安裝它。然后,當 Visual Studio 建議安裝它時:
'192.168.1.180' 上不存在受支持的 CMake 版本。從 CMake.org 安裝最新的 CMake 二進制檔案?
如果您同意這樣做,它實際上會在所述檔案夾中本地推出它。如果您嘗試在本地 Mac 機器上使用它,Visual Studio 使用的二進制檔案已損壞并引發錯誤:
$: ~/.vs/cmake/bin/cmake
zsh: exec format error: /Users/User/.vs/cmake/bin/cmake
這就是為什么 Visual Studio 一直在努力尋找有效的 CMake 二進制檔案的原因。您可以通過使用作業 CMake 二進制檔案創建一個指向檔案夾的符號鏈接來代替 Visual Studio 在其中查找它們的檔案夾來獲得它:
$: 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 可以使用的快取,但是請注意,每當此快取因任何原因(例如在配置之間切換時)無效時,您必須以相同的方式再次重新生成它。另一種選擇是明確指定所有缺少的引數(通過CMakeLists.txt或作為CMakePresets.jsoncacheVariables屬性下的命令列引數)
uj5u.com熱心網友回復:
我終于解決了這個問題,我不得不將位于 CMake 應用程式中的檔案復制/Applications/CMake.app/Contents/bin/cmake到 Visual Studio 試圖找到它們的位置,在我的例子中是:/Users/maria/.vs/cmake/bin/cmake
該版本不是問題,因為我使用最新的 CMake 版本 (3.23.1) 進行了嘗試,并且可以正常作業。最后我發現了一個與 C 和 CMake 的編譯器的位置以及 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 上開發時會發現這是一個有用的解決方案
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/480252.html
