Issue
I'm very new to CMake (and new to C++ too, although that shouldn't matter here), and I am having a problem using CMake with Visual studio.
I have created a directory, let's say it's called Project
, and put in it a simple project with the following structure:
Project/
build/ <empty>
src/
main.cpp
CMakeLists.txt
CMakePresets.json
Inside these files is just the most basic, default code:
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
(this code is just the default that was generated):{ "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; }
Then, I have used CMake to create a Visual Studio solution:
C:\...\Project\build> cmake ..
This has worked fine without any errors, and Visual Studio can open the solution. It can also build the project correctly...
But it cannot run the executable which it has built. After successfully building the project, it has written the executable to C:\...\Project\build\Debug\Project.exe
, but it tries to open C:\...\Project\build\x64\Debug\ALL_BUILD
instead, and I get an error popup.
I gather that there are two things wrong here:
- The executable file should be written within the
C:\...\Project\build\x64\Debug
folder, not just theC:\...\Project\build\Debug
folder. This is how it has worked whenever I have used Visual Studio before, and this is the folder it is trying to search in. - It should be searching for an executable called
Project.exe
, not one calledALL_BUILD
.
When I run Project.exe
manually from the command line, it works fine. But I cannot seem to make Visual Studio run it correctly.
What have I done wrong here, and how can I get this to work?
Solution
Default project is set to ALL_BUILD
to change the default for the VS generators use the following CMake statement:
set_property(DIRECTORY PROPERTY VS_STARTUP_PROJECT Project)
Anywhere after the add_executable
command.
Answered By - ixSci Answer Checked By - Candace Johnson (WPSolving Volunteer)