Visual Studio deploys CMake environment connected to remote WSL

demand

Although CMAKE is "cross platform", it actually refers to the problem of APIs. My requirements here do not involve the problem of multiple APIs. I only use "cross development platform". That is, cross compiling and developing Linux software under Windows (the build platform is different, but the host platform is the same)

We often have a problem when developing Linux software, that is, debugging. Windows, Visual Studio or Visual Studio Code, provides very friendly one-step debugging function. Therefore, we need a way to develop Linux software under windows

After a period of development, I think WSL2 + Visual Studio remote + CMake tool chain is more convenient. I have also used Visual Studio Code, not to mention the configuration of various json files. In fact, the direct step-by-step debugging is slightly inferior to visual studio. The latter is very convenient to view memory during debugging, and the former depends on plug-ins. In essence, it is the difference between IDE and editor, but no matter which one you choose, it is a tool and you can use it easily



Environment introduction

WSL2 environment:

# Kernel version
[root@xxx]# uname -a
Linux xxx 5.10.16.3-microsoft-standard-WSL2 #1 SMP Fri Apr 2 22:23:49 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

# Release version
[root@xxx]# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.4 LTS
Release:        20.04
Codename:       focal

Since the remote API is used, the Windows SDK version is meaningless. Only the Visual Studio version is provided here:

Microsoft Visual Studio Community 2022 (64-bit) - Current Version 17.2.6

This is the remote (i.e. WSL2) development environment. You should first refer to Microsoft official cross compilation tool chain requirements

gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
g++ (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
GNU gdb (Ubuntu 10.2-0ubuntu1~20.04~1) 10.2
GNU Make 4.2.1
ninja 1.10.0
rsync  version 3.1.3
Zip 3.0
cmake version 3.23.2



Environment deployment record

The following is the folder organization. There is only one source file and one CMakeLists.txt:

According to Microsoft documents, if there is a cmakelists Txt folder can be recognized by visual studio, so open Visual Studio

Select the folder path just now

Wait a moment and the following page will be displayed. The places circled by the red box will be used when configuring CMake configuration

Now let's focus on the red box in the lower left corner. The output log here probably means that Visual Studio has detected that this is a CMake project, so cmakepreset is generated by default JSON file (this is the default file of CMake). However, this "default" configuration is not required for cross compilation because it is a configuration under Windows.

However, before modifying the configuration file, please add one content. That's cmakepreset JSON, which was introduced after Visual Studio 2019. The previous version was cmakesettings JSON, these are all one thing. Now that we are using Visual Studio 2022, we will directly use cmakepreset JSON, which also needs to be modified, click [tools] - [options]:

Find the [cmake] - [general] tab and check "always use CMakePreset.json" in the [configuration file] column:

Now click the red box in the middle to enter cmakepreset JSON is configured; Otherwise, you will use cmakesettings without modification JSON file:

The default opening is as follows. There are many configurations under the "configuration" key:

I don't need them here, delete them all, and only keep the following two configurations, one Debug and one Release. For the meaning of these configuration key value pairs, please refer to CMake official configuration document:

{
  "version": 3,
  "configurePresets": [
    {
      "name": "linux-debug",
      "displayName": "Linux Debug",
      "generator": "Ninja",
      "binaryDir": "${sourceDir}/out/build/${presetName}",
      "installDir": "${sourceDir}/out/install/${presetName}",
      "cacheVariables": { "CMAKE_BUILD_TYPE": "Debug" },
      "condition": {
        "type": "equals",
        "lhs": "${hostSystemName}",
        "rhs": "Linux"
      }
    },
    {
      "name": "linux-release",
      "displayName": "Linux Release",
      "generator": "Ninja",
      "binaryDir": "${sourceDir}/out/build/${presetName}",
      "installDir": "${sourceDir}/out/install/${presetName}",
      "cacheVariables": { "CMAKE_BUILD_TYPE": "Release" },
      "condition": {
        "type": "equals",
        "lhs": "${hostSystemName}",
        "rhs": "Linux"
      }
    }
  ]
}

When you finish modifying and press "Ctrl S", the CMake output window in the lower left corner will also be updated:

However, note that the configuration file displayed on the toolbar is still x64 Debug, which means that Visual Studio has not yet used this configuration. You need to modify the local machine to the remote WSL2, and click the red box at the top:

After that, the two places will change immediately:

Then wait until the CMake output window tells you that CMake generation finished You will find that the configuration file is the cmakepreset JSON:

So far, the CMake build system has been deployed. What is this reflected in?

First, there will be an out directory in your Visual Studio, which is the same as your cmakepreset JSON configuration (as for why there is a folder named by a negative number in the out directory, all I know so far is that it is related to the remote connection. If you are a local developer, you will not have a folder with this name) (however, in fact, Visual Studio calls the remote development environment by rsync synchronization, so there will be an additional copy in a directory in your remote WSL2 environment)

Second, click "target view", and you will find that a target file has been generated:

When these two phenomena occur, the CMake build system is effective



However, it is not enough. We also need to debug, so we need to write a configuration file

First, right-click the target file of the target view:

Select to add a Debug configuration file, and then a window will open, which is the launch.vs.json configuration file. For specific key value pairs, please refer to Microsoft manual See what they mean, but simply change the default configuration:

{
  "version": "0.2.1",
  "defaults": {},
  "configurations": [
    {
      "type": "cppgdb",
      "name": "test",
      "project": "CMakeLists.txt",
      "projectTarget": "test",
      "debuggerConfiguration": "gdb",
      "args": [],
      "env": {}
    }
  ]
}

Now press "F5" to debug happily:

In addition, if you want to use std::cin to input, just enter it in the red box above, and then press enter

In addition, if an error is reported after pressing "F5", for example, the remote server cannot be connected, it is likely that your remote WSL2 environment is not set properly, for example, other dependencies are missing. Please check the remote development environment



Tags: Visual Studio

Posted by guiltyspark on Mon, 15 Aug 2022 01:34:06 +0930