我對 CMake 很陌生(對 C 也很陌生,盡管這在這里無關緊要),我在使用 CMake 和 Visual Studio 時遇到了問題。
我創建了一個目錄,假設它被稱為Project,并在其中放入一個具有以下結構的簡單專案:
Project/
build/ <empty>
src/
main.cpp
CMakeLists.txt
CMakePresets.json
這些檔案中只是最基本的默認代碼:
CMakeLists.txt:cmake_minimum_required (VERSION 3.8) project (Project) set (CMAKE_CXX_STANDARD 20) set (CMAKE_CXX_STANDARD_REQUIRED True) add_executable (Project src/main.cpp)CMakePresets.json(此代碼只是生成的默認代碼):{ "version": 3, "configurePresets": [ { "name": "windows-base", "hidden": true, "generator": "Ninja", "binaryDir": "${sourceDir}/out/build/${presetName}", "installDir": "${sourceDir}/out/install/${presetName}", "cacheVariables": { "CMAKE_C_COMPILER": "cl.exe", "CMAKE_CXX_COMPILER": "cl.exe" }, "condition": { "type": "equals", "lhs": "${hostSystemName}", "rhs": "Windows" } }, { "name": "x64-debug", "displayName": "x64 Debug", "inherits": "windows-base", "architecture": { "value": "x64", "strategy": "external" }, "cacheVariables": { "CMAKE_BUILD_TYPE": "Debug" } }, { "name": "x64-release", "displayName": "x64 Release", "inherits": "x64-debug", "cacheVariables": { "CMAKE_BUILD_TYPE": "Release" } }, { "name": "x86-debug", "displayName": "x86 Debug", "inherits": "windows-base", "architecture": { "value": "x86", "strategy": "external" }, "cacheVariables": { "CMAKE_BUILD_TYPE": "Debug" } }, { "name": "x86-release", "displayName": "x86 Release", "inherits": "x86-debug", "cacheVariables": { "CMAKE_BUILD_TYPE": "Release" } } ] }src/main.cpp:#include <iostream> int main() { std::cout << "Hello, world!" << std::endl; return 0; }
然后,我使用 CMake 創建了一個 Visual Studio 解決方案:
C:\...\Project\build> cmake ..
這作業正常,沒有任何錯誤,Visual Studio 可以打開解決方案。它還可以正確構建專案...
但它無法運行它構建的可執行檔案。成功構建專案后,它已將可執行檔案寫入C:\...\Project\build\Debug\Project.exe,但它嘗試打開C:\...\Project\build\x64\Debug\ALL_BUILD,我收到一個錯誤彈出視窗。
我認為這里有兩件事是錯誤的:
- 可執行檔案應寫入
C:\...\Project\build\x64\Debug檔案夾,而不僅僅是C:\...\Project\build\Debug檔案夾。這就是我以前使用 Visual Studio 時的作業方式,這是它試圖搜索的檔案夾。 - 它應該搜索一個名為的可執行檔案
Project.exe,而不是一個名為ALL_BUILD.
當我從命令列Project.exe手動運行時,它作業正常。但我似乎無法讓 Visual Studio 正確運行它。
我在這里做錯了什么,我怎樣才能讓它作業?
uj5u.com熱心網友回復:
默認專案設定為ALL_BUILD更改 VS 生成器的默認值,使用以下 CMake 陳述句:
set_property(DIRECTORY PROPERTY VS_STARTUP_PROJECT Project)
命令后的任何地方add_executable。
uj5u.com熱心網友回復:
您會在解決方案資源管理器中看到幾個專案。這些專案是構建目標。默認目標是ALL_BUILD,它構建所有配置的目標,就像cmake --build .它一樣。
在解決方案資源管理器中將所需目標設定為啟動專案。這將指向除錯器要運行的可執行檔案。

轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/484612.html
上一篇:型別'{描述:字串;}'缺少型別'IOrderFormWithReactProps'的以下屬性:context、siteUrlts(2769)
