|
| 1 | +--- |
| 2 | +title: "Configure CMake settings for Linux in Visual Studio" |
| 3 | +description: "How to configure Linux CMake project in Visual Studio" |
| 4 | +ms.date: "07/23/2020" |
| 5 | +--- |
| 6 | +# Configure CMake settings for Linux in Visual Studio |
| 7 | + |
| 8 | +::: moniker range="vs-2015" |
| 9 | +Linux support is available in Visual Studio 2017 and later. To see the documentation for these versions, set the Visual Studio **Version** selector control for this article to Visual Studio 2017 or Visual Studio 2019. It's found at the top of the table of contents on this page. |
| 10 | +::: moniker-end |
| 11 | + |
| 12 | +::: moniker range=">=vs-2017" |
| 13 | +This topic describes how to configure a C++ Linux project as described in [Create a Linux CMake project in Visual Studio](cmake-linux-project.md). For MSBuild Linux projects, see [Configure a Linux Project](configure-a-linux-project.md) |
| 14 | + |
| 15 | +A *CMakeSettings.json* file in a CMake Linux project can specify all the properties listed in [Customize CMake settings](../build/customize-cmake-settings.md), plus additional properties that control the build settings on the remote Linux machine. |
| 16 | +::: moniker-end |
| 17 | + |
| 18 | +::: moniker range="vs-2019" |
| 19 | +To change the default CMake settings in Visual Studio 2019, from the main toolbar, open the **Configuration** dropdown and choose **Manage Configurations**. |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +This command brings up the **CMake Settings Editor**, which you can use to edit the *CMakeSettings.json* file in your root project folder. You can also open the file directly by clicking the **Edit JSON** button in the editor. For more information, see [Customize CMake Settings](../build/customize-cmake-settings.md). |
| 24 | + |
| 25 | +The the default Linux-Debug configuration in Visual Studio 2019 version 16.1 and later is shown here: |
| 26 | + |
| 27 | +```json |
| 28 | +{ |
| 29 | + "name": "Linux-Debug", |
| 30 | + "generator": "Unix Makefiles", |
| 31 | + "configurationType": "Debug", |
| 32 | + "cmakeExecutable": "/usr/bin/cmake", |
| 33 | + "remoteCopySourcesExclusionList": [ ".vs", ".git", "out" ], |
| 34 | + "cmakeCommandArgs": "", |
| 35 | + "buildCommandArgs": "", |
| 36 | + "ctestCommandArgs": "", |
| 37 | + "inheritEnvironments": [ "linux_x64" ], |
| 38 | + "remoteMachineName": "${defaultRemoteMachineName}", |
| 39 | + "remoteCMakeListsRoot": "$HOME/.vs/${projectDirName}/${workspaceHash}/src", |
| 40 | + "remoteBuildRoot": "$HOME/.vs/${projectDirName}/${workspaceHash}/out/build/${name}", |
| 41 | + "remoteInstallRoot": "$HOME/.vs/${projectDirName}/${workspaceHash}/out/install/${name}", |
| 42 | + "remoteCopySources": true, |
| 43 | + "rsyncCommandArgs": "-t --delete --delete-excluded", |
| 44 | + "remoteCopyBuildOutput": false, |
| 45 | + "remoteCopySourcesMethod": "rsync", |
| 46 | + "addressSanitizerRuntimeFlags": "detect_leaks=0", |
| 47 | + "variables": [] |
| 48 | + } |
| 49 | + ] |
| 50 | +} |
| 51 | +``` |
| 52 | +::: moniker-end |
| 53 | +::: moniker range="vs-2017" |
| 54 | +To change the default CMake settings in Visual Studio 2017, choose **CMake** > **Change CMake Settings** > **CMakeLists.txt** from the main menu. Or, right-click *CMakeSettings.txt* in **Solution Explorer** and choose **Change CMake Settings**. Visual Studio then creates a new *CMakeSettings.json* file in your root project folder. You can open the file using the **CMake Settings** editor or modify the file directly. For more information, see [Customize CMake settings](../build/customize-cmake-settings.md). |
| 55 | + |
| 56 | +Given the following code and CMakeLists.txt file: |
| 57 | + |
| 58 | +```cpp |
| 59 | +// hello.cpp |
| 60 | + |
| 61 | +#include <iostream> |
| 62 | + |
| 63 | +int main(int argc, char* argv[]) |
| 64 | +{ |
| 65 | + std::cout << "Hello from Linux CMake" << std::endl; |
| 66 | +} |
| 67 | +``` |
| 68 | +
|
| 69 | +*CMakeLists.txt*: |
| 70 | +
|
| 71 | +```txt |
| 72 | +cmake_minimum_required(VERSION 3.8) |
| 73 | +project (hello-cmake) |
| 74 | +add_executable(hello-cmake hello.cpp) |
| 75 | +``` |
| 76 | + |
| 77 | +You would see something like this default configuration for Linux-Debug in Visual Studio 2017 (and Visual Studio 2019 version 16.0): |
| 78 | + |
| 79 | +```json |
| 80 | +{ |
| 81 | + "name": "Linux-Debug", |
| 82 | + "generator": "Unix Makefiles", |
| 83 | + "remoteMachineName": "${defaultRemoteMachineName}", |
| 84 | + "configurationType": "Debug", |
| 85 | + "remoteCMakeListsRoot": "/var/tmp/src/${workspaceHash}/${name}", |
| 86 | + "cmakeExecutable": "/usr/local/bin/cmake", |
| 87 | + "buildRoot": "${env.LOCALAPPDATA}\\CMakeBuilds\\${workspaceHash}\\build\\${name}", |
| 88 | + "installRoot": "${env.LOCALAPPDATA}\\CMakeBuilds\\${workspaceHash}\\install\\${name}", |
| 89 | + "remoteBuildRoot": "/var/tmp/build/${workspaceHash}/build/${name}", |
| 90 | + "remoteInstallRoot": "/var/tmp/build/${workspaceHash}/install/${name}", |
| 91 | + "remoteCopySources": true, |
| 92 | + "remoteCopySourcesOutputVerbosity": "Normal", |
| 93 | + "remoteCopySourcesConcurrentCopies": "10", |
| 94 | + "remoteCopySourcesMethod": "rsync", |
| 95 | + "remoteCopySourcesExclusionList": [".vs", ".git"], |
| 96 | + "rsyncCommandArgs" : "-t --delete --delete-excluded", |
| 97 | + "remoteCopyBuildOutput" : "false", |
| 98 | + "cmakeCommandArgs": "", |
| 99 | + "buildCommandArgs": "", |
| 100 | + "ctestCommandArgs": "", |
| 101 | + "inheritEnvironments": [ "linux-x64" ] |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +::: moniker-end |
| 106 | +::: moniker range=">=vs-2017" |
| 107 | +For more information about these settings, see [CMakeSettings.json reference](../build/cmakesettings-reference.md). |
| 108 | + |
| 109 | +## Optional Settings |
| 110 | + |
| 111 | +You can use the following optional settings for more control: |
| 112 | + |
| 113 | +```json |
| 114 | +{ |
| 115 | + "remotePrebuildCommand": "", |
| 116 | + "remotePreGenerateCommand": "", |
| 117 | + "remotePostbuildCommand": "", |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +These options allow you to run commands on the Linux system before and after building, and before CMake generation. The values can be any command that is valid on the remote system. The output is piped back to Visual Studio. |
| 122 | + |
| 123 | +## Next step |
| 124 | + |
| 125 | +[Debug a CMake Linux project](cmake-linux-debug.md) |
| 126 | + |
| 127 | +## See also |
| 128 | + |
| 129 | +[Working with Project Properties](../build/working-with-project-properties.md)<br/> |
| 130 | +[CMake Projects in Visual Studio](../build/cmake-projects-in-visual-studio.md)<br/> |
| 131 | +[Connect to your remote Linux computer](connect-to-your-remote-linux-computer.md)<br/> |
| 132 | +[Customize CMake settings](../build/customize-cmake-settings.md) |
| 133 | +<br/> |
| 134 | +[CMake predefined configuration reference](../build/cmake-predefined-configuration-reference.md) |
| 135 | +::: moniker-end |
0 commit comments