Visual Studio configures remote development and debugging of Linux projects

1. Which project templates can be selected

There are two easy-to-use ways to use visual studio to develop Linux projects remotely, one is only suitable for Linux system projects , and the other is cross-platform C++ projects .

2. C++ projects only applicable to Linux systems

This method is relatively simple. We use this method because we value the powerful debugging function of visual studio.
Proceed as follows:

  1. To create a new project
    template, the platform must be selected as Linux , here we choose an empty project.
    create new project
  2. Configure Remote Server
    Tools->Options->Cross-Platform->Connection Manager->Add
    Configure remote server
  3. Configuration Properties
    Configure Remote Build Directory
    configuration properties
  4. Breakpoint debugging
    Next, you can happily add breakpoints in vs for debugging.
    You can see that the corresponding process and gdb process will also run on the remote Linux server.
    vs breakpoint debugging
    Corresponding process in remote Linux
    If it is a large-scale multi-threaded project and has not been used, I don't know if there is a pit!

3. Cross-platform c++ project (cmake)

Use cmake for project builds instead of relying on Microsoft's sln for builds. It can be debugged in both local windows and remote Linux, and it is very convenient to do cross-platform development. The premise is to use cmake~~. Here is a good CMake tutorial
"CMake Tutorial"
The steps are as follows:

  1. Create new project
    Select CMake项目, this kind of project will not be generated again .slnor .vcxproj.
  2. Manage Configuration
    Configuration->Manage Configuration, click to open CMakePresets.jsonthe file.
    The Linux-debug item is the attribute configuration related to our remote debugging.
    The example is as follows
{
        "name": "linux-debug", #会在调试框中显现的名称
        "displayName": "Linux Debug",
        "generator": "Unix Makefiles",
        "binaryDir": "${sourceDir}/out/build/${presetName}",
        "installDir": "${sourceDir}/out/install/${presetName}",
        "cmakeExecutable": "/usr/bin/cmake", #填写远程的CMake路径
        "cacheVariables": {
          "CMAKE_BUILD_TYPE": "Debug"
        },
        "condition": {
          "type": "equals",
          "lhs": "${hostSystemName}",
          "rhs": "Linux"
        },
        "vendor": {
          "microsoft.com/VisualStudioRemoteSettings/CMake/1.0": {
            //"sourceDir": "$env{HOME}/.vs/$ms{projectDirName}"
            "sourceDir": "/var/tmp" #本机代码复制到远程的目录 如果不设置默认用上一行的路径,也就似乎 ${HOME}下的.vs目录,此例中是改为了/var/tmp目录
          }
        }
      
  1. Remote operation
    Write code to judge the platform type, introduce different header files, and run different functions to achieve the effect of process dormancy.
    Windows platform can use the Sleep function in windosw.h;
    Linux platform can use the sleep function in unistd.h.
    You can also use the sleep function in c++11 to achieve a cross-platform effect and save the judgment of the platform.
    "C++ cross-platform judges the current operating system windows, linux and compiler through macro definition"
    macro definition determination system
  2. Local Execution
    Local execution is self-explanatory.
    The local project is also built through cmake.
    The mastery of cmake is the key.

Guess you like

Origin blog.csdn.net/sinat_36304757/article/details/124653732