|
| 1 | +--- |
| 2 | +title: "Customize CMake settings in Visual Studio" |
| 3 | +ms.date: "11/08/2018" |
| 4 | +helpviewer_keywords: ["CMake settings"] |
| 5 | +--- |
| 6 | +# Customizing CMake settings |
| 7 | + |
| 8 | +By default, Visual Studio provides six default CMake configurations ("x86-Debug", "x86-Release", "x64-Debug", "x64-Release", "Linux-Debug" and "Linux-Release"). These configurations define how CMake.exe is invoked to create the CMake cache for a given project. To modify these configurations, or create a new custom configuration, choose **CMake | Change CMake Settings**, and then choose the CMakeLists.txt file that the settings apply to. The **Change CMake Settings** command is also available on the file's context menu in **Solution Explorer**. This command creates a CMakeSettings.json file in the project folder. This file is used to re-create the CMake cache file, for example after a **Clean** operation. |
| 9 | + |
| 10 | +  |
| 11 | + |
| 12 | +JSON IntelliSense helps you edit the CMakeSettings.json file: |
| 13 | + |
| 14 | +  |
| 15 | + |
| 16 | +The following example shows a sample configuration, which you can use as the starting point to create your own in CMakeSettings.json: |
| 17 | + |
| 18 | +```json |
| 19 | + { |
| 20 | + "name": "x86-Debug", |
| 21 | + "generator": "Ninja", |
| 22 | + "configurationType": "Debug", |
| 23 | + "inheritEnvironments": [ "msvc_x86" ], |
| 24 | + "buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}", |
| 25 | + "installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}", |
| 26 | + "cmakeCommandArgs": "", |
| 27 | + "buildCommandArgs": "-v", |
| 28 | + "ctestCommandArgs": "" |
| 29 | + }, |
| 30 | + |
| 31 | +``` |
| 32 | + |
| 33 | +1. **name**: the name that appears in the C++ configuration dropdown. This property value can also be used as a macro, `${name}`, to specify other property values. For an example, see the **buildRoot** definition in CMakeSettings.json. |
| 34 | + |
| 35 | +1. **generator**: maps to the **-G** switch and specifies the generator to be used. This property can also be used as a macro, `${generator}`, to help specify other property values. Visual Studio currently supports the following CMake generators: |
| 36 | + |
| 37 | + - "Ninja" |
| 38 | + - "Visual Studio 14 2015" |
| 39 | + - "Visual Studio 14 2015 ARM" |
| 40 | + - "Visual Studio 14 2015 Win64" |
| 41 | + - "Visual Studio 15 2017" |
| 42 | + - "Visual Studio 15 2017 ARM" |
| 43 | + - "Visual Studio 15 2017 Win64" |
| 44 | + |
| 45 | + Because Ninja is designed for fast build speeds instead of flexibility and function, it is set as the default. However, some CMake projects may be unable to correctly build using Ninja. If this occurs, you can instruct CMake to generate a Visual Studio project instead. |
| 46 | + |
| 47 | + To specify a Visual Studio generator, open the CMakeSettings.json from the main menu by choosing **CMake | Change CMake Settings**. Delete “Ninja” and type “V”. This activates IntelliSense, which enables you to choose the generator you want. |
| 48 | + |
| 49 | +1. **buildRoot**: maps to **-DCMAKE_BINARY_DIR** switch and specifies where the CMake cache will be created. If the folder does not exist, it is created. |
| 50 | + |
| 51 | +1. **variables**: contains a name-value pair of CMake variables that will get passed as **-D** *_name_=_value_* to CMake. If your CMake project build instructions specify the addition of any variables directly to the CMake cache file, it is recommended that you add them here instead. The following example shows how to specify the name-value pairs: |
| 52 | + |
| 53 | +```json |
| 54 | +"variables": [ |
| 55 | + { |
| 56 | + "name": "CMAKE_CXX_COMPILER", |
| 57 | + "value": "C:/Program Files (x86)/Microsoft Visual Studio/157/Enterprise/VC/Tools/MSVC/14.14.26428/bin/HostX86/x86/cl.exe" |
| 58 | + }, |
| 59 | + { |
| 60 | + "name": "CMAKE_C_COMPILER", |
| 61 | + "value": "C:/Program Files (x86)/Microsoft Visual Studio/157/Enterprise/VC/Tools/MSVC/14.14.26428/bin/HostX86/x86/cl.exe" |
| 62 | + } |
| 63 | + ] |
| 64 | +``` |
| 65 | + |
| 66 | +1. **cmakeCommandArgs**: specifies any additional switches you want to pass to CMake.exe. |
| 67 | + |
| 68 | +1. **configurationType**: defines the build configuration type for the selected generator. Currently supported values are "Debug", "MinSizeRel", "Release", and "RelWithDebInfo". |
| 69 | + |
| 70 | +1. **ctestCommandArgs**: specifies additional switches to pass to CTest when running tests. |
| 71 | + |
| 72 | +1. **buildCommandArgs**: specifies additional switches to pass to the underlying build system. For example, passing -v when using the Ninja generator forces Ninja to output command lines. |
| 73 | + |
| 74 | +## Environment variables |
| 75 | + |
| 76 | +CMakeSettings.json also supports consuming environment variables in any of the properties mentioned above. The syntax to use is `${env.FOO}` to expand the environment variable %FOO%. |
| 77 | +You also have access to built-in macros inside this file: |
| 78 | + |
| 79 | +- `${workspaceRoot}` – provides the full path of the workspace folder |
| 80 | +- `${workspaceHash}` – hash of workspace location; useful for creating a unique identifier for the current workspace (for example, to use in folder paths) |
| 81 | +- `${projectFile}` – the full path of the root CMakeLists.txt file |
| 82 | +- `${projectDir}` – the full path of the folder of the root CMakeLists.txt file |
| 83 | +- `${thisFile}` – the full path of the CMakeSettings.json file |
| 84 | +- `${name}` – the name of the configuration |
| 85 | +- `${generator}` – the name of the CMake generator used in this configuration |
| 86 | + |
| 87 | +## Ninja command line arguments |
| 88 | + |
| 89 | +If targets are unspecified, builds the 'default' target (see manual). |
| 90 | + |
| 91 | +```cmd |
| 92 | +C:\Program Files (x86)\Microsoft Visual Studio\Preview\Enterprise>ninja -? |
| 93 | +ninja: invalid option -- `-?' |
| 94 | +usage: ninja [options] [targets...] |
| 95 | +``` |
| 96 | + |
| 97 | +|Option|Description| |
| 98 | +|--------------|------------| |
| 99 | +| --version | print ninja version ("1.7.1")| |
| 100 | +| -C DIR | change to DIR before doing anything else| |
| 101 | +| -f FILE | specify input build file (default=build.ninja)| |
| 102 | +| -j N | run N jobs in parallel (default=14, derived from CPUs available)| |
| 103 | +| -k N | keep going until N jobs fail (default=1)| |
| 104 | +| -l N | do not start new jobs if the load average is greater than N| |
| 105 | +| -n | dry run (don't run commands but act like they succeeded)| |
| 106 | +| -v | show all command lines while building| |
| 107 | +| -d MODE | enable debugging (use -d list to list modes)| |
| 108 | +| -t TOOL | run a subtool (use -t list to list subtools). terminates toplevel options; further flags are passed to the tool| |
| 109 | +| -w FLAG | adjust warnings (use -w list to list warnings)| |
| 110 | + |
| 111 | +## Inherited environments (Visual Studio 2017 version 15.5) |
| 112 | + |
| 113 | +CMakeSettings.json now supports inherited environments. This feature enables you to (1) inherit default environments and (2) create custom environment variables that are passed to CMake.exe when it runs. |
| 114 | + |
| 115 | +```json |
| 116 | + "inheritEnvironments": [ "msvc_x64_x64" ] |
| 117 | +``` |
| 118 | + |
| 119 | +The example above is the same as running the **Developer Command Prompt for VS 2017** with the **-arch=amd64 -host_arch=amd64** arguments. |
| 120 | + |
| 121 | +The following table shows the default values: |
| 122 | + |
| 123 | +|Context Name|Description| |
| 124 | +|-----------|-----------------| |
| 125 | +|vsdev|The default Visual Studio environment| |
| 126 | +|msvc_x86|Compile for x86 using x86 tools| |
| 127 | +|msvc_arm| Compile for ARM using x86 tools| |
| 128 | +|msvc_arm64|Compile for ARM64 using x86 tools| |
| 129 | +|msvc_x86_x64|Compile for AMD64 using x86 tools| |
| 130 | +|msvc_x64_x64|Compile for AMD64 using 64-bit tools| |
| 131 | +|msvc_arm_x64|Compile for ARM using 64-bit tools| |
| 132 | +|msvc_arm64_x64|Compile for ARM64 using 64-bit tools| |
| 133 | + |
| 134 | +### Custom environment variables |
| 135 | + |
| 136 | +In CMakeSettings.json, you can define custom environment variables globally or per-configuration in the **environments** property. The following example defines one global variable, **BuildDir**, which is inherited in both the x86-Debug and x64-Debug configurations. Each configuration uses the variable to specify the value for the **buildRoot** property for that configuration. Note also how each configuration uses the **inheritEnvironments** property to specify a variable that applies only to that configuration. |
| 137 | + |
| 138 | +```json |
| 139 | +{ |
| 140 | + // The "environments" property is an array of key value pairs of the form |
| 141 | + // { "EnvVar1": "Value1", "EnvVar2": "Value2" } |
| 142 | + "environments": [ |
| 143 | + { |
| 144 | + "BuildDir": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build", |
| 145 | + } |
| 146 | + ], |
| 147 | + |
| 148 | + "configurations": [ |
| 149 | + { |
| 150 | + "name": "x86-Debug", |
| 151 | + "generator": "Ninja", |
| 152 | + "configurationType": "Debug", |
| 153 | + // Inherit the defaults for using the MSVC x86 compiler. |
| 154 | + "inheritEnvironments": [ "msvc_x86" ], |
| 155 | + "buildRoot": "${env.BuildDir}\\${name}" }, |
| 156 | + { |
| 157 | + "name": "x64-Debug", |
| 158 | + "generator": "Ninja", |
| 159 | + "configurationType": "Debug", |
| 160 | + // Inherit the defaults for using the MSVC x64 compiler. |
| 161 | + "inheritEnvironments": [ "msvc_x64" ], |
| 162 | + "buildRoot": "${env.BuildDir}\\${name}" |
| 163 | + } |
| 164 | + ] |
| 165 | +} |
| 166 | +``` |
| 167 | + |
| 168 | +In the next example, the x86-Debug configuration defines its own value for the **BuildDir** property, and this value overrides the value set by the global **BuildDir** property so that **BuildRoot** evaluates to `D:\custom-builddir\x86-Debug`. |
| 169 | + |
| 170 | +```json |
| 171 | +{ |
| 172 | + "environments": [ |
| 173 | + { |
| 174 | + "BuildDir": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}", |
| 175 | + } |
| 176 | + ], |
| 177 | + |
| 178 | + "configurations": [ |
| 179 | + { |
| 180 | + "name": "x86-Debug", |
| 181 | + |
| 182 | + // The syntax for this property is the same as the global one above. |
| 183 | + "environments": [ |
| 184 | + { |
| 185 | + // Replace the global property entirely. |
| 186 | + "BuildDir": "D:\\custom-builddir", |
| 187 | + } |
| 188 | + ], |
| 189 | + |
| 190 | + "generator": "Ninja", |
| 191 | + "configurationType": "Debug", |
| 192 | + "inheritEnvironments": [ "msvc_x86" ], |
| 193 | + // Evaluates to "D:\custom-builddir\x86-Debug" |
| 194 | + "buildRoot": "${env.BuildDir}\\${name}" |
| 195 | + }, |
| 196 | + { |
| 197 | + "name": "x64-Debug", |
| 198 | + |
| 199 | + "generator": "Ninja", |
| 200 | + "configurationType": "Debug", |
| 201 | + "inheritEnvironments": [ "msvc_x64" ], |
| 202 | + // Since this configuration doesn’t modify BuildDir, it inherits |
| 203 | + // from the one defined globally. |
| 204 | + "buildRoot": "${env.BuildDir}\\${name}" |
| 205 | + } |
| 206 | + ] |
| 207 | +} |
| 208 | +``` |
0 commit comments