简体   繁体   中英

Visual Studio 2017 and CMake - How to set the build directory

I use Visual Studio 2017. I have noticed that the CMake build directory is placed in some place other than my project folder:

C:\Users\User\CMakeBuilds\"some id"

I would like to have the CMake build directory in my project folder. How can I achieve this?

Edit: I have just created a basic CMakeLists.txt using notepad:

cmake_minimum_required(VERSION 3.8)
project (TestCpp)
add_executable(testCpp main.cpp)

I open it in Visual Studio using the File > Open > Folder options.

From the documentation on the Microsoft website, you can change your Build directory by modifying the CMakeSettings.json file. This file will be located in your root project directory, so you can open it by double-clicking it in the Solution Explorer . It may show the CMake Settings screen, so you can click the highlighted CMakeSettings.json text at the top to edit the underlying file. It should look something like this:

{
  "name": "x86-Debug",
  "generator": "Ninja",
  "configurationType": "Debug",
  "inheritEnvironments": [ "msvc_x86" ],
  "buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
  "installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
  "cmakeCommandArgs": "",
  "buildCommandArgs": "-v",
  "ctestCommandArgs": ""
},

So just change the buildRoot value to where you want Visual Studio to place your Build directory, save your changes, and Visual Studio should automatically re-run CMake to apply the changes.

On some configurations, the buildRoot already defaults to a location within your project:

  "buildRoot": "${projectDir}\\out\\build2\\${name}",

but in other cases, it will use a path in the current user's home directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM