vscode在连接远程调试时不知道如何调试cmake,研究了半天没研究出来,因此决定使用visual studio进行调试。
Linux端
cmake版本最低为3.11,可以从此网站下载:https://cmake.org/files/v3.11/
之前安装的visual studio是2019版本,研究了一天发现只能连接ssh做非cmake工程的开发。要连接远程cmake工程的话要安装2022版本 (2019不能用,我直接试的2022版本)。
安装的时候选择以下两项即可。

配置
点击工具–选项,打开“选项”对话框,选择跨平台标签,添加一个远程链接。

再选择 "CMake—常规 勾选从不使用CMake预设

创建项目
选择文件–>创建新项目,选择CMake项目即可。


CMakePresets.json文件
CMakeSettings.json文件。
红叉删除默认的配置,再点绿色的加号重新添加一个配置。

{"configurations": [{"name": "Remote_Linux","generator": "Ninja","configurationType": "Debug","cmakeExecutable": "cmake","remoteCopySourcesExclusionList": [ ".vs", ".git", "out" ],"cmakeCommandArgs": "","buildCommandArgs": "","ctestCommandArgs": "","inheritEnvironments": [ "linux_x64" ],"remoteMachineName": "${defaultRemoteMachineName}","remoteCMakeListsRoot": "$HOME/.vs/${projectDirName}","remoteBuildRoot": "$HOME/.vs/${projectDirName}/build/","remoteInstallRoot": "$HOME/.vs/${projectDirName}/install","remoteCopySources": true,"rsyncCommandArgs": "-t --delete","remoteCopyBuildOutput": false,"remoteCopySourcesMethod": "rsync","intelliSenseMode": "linux-gcc-x64"}]
}
CMakeLists.txt文件上右击鼠标,选择添加调试信息,如下图。
gdbserver启动配置。
launch.vs.json文件。{"version": "0.2.1","defaults": {},"configurations": [{"type": "cppgdb","name": "CMakeLists.txt","project": "CMakeLists.txt","projectTarget": "CMakeProject1","comment": "了解如何配置远程调试。有关详细信息,请参阅 http://aka.ms/vslinuxdebug","debuggerConfiguration": "gdbserver","args": [],"env": {}}]
}
projectTarget参数上写CMakeProject1,此文件名来源于源码CMakeLists.txt文件中的add_executable后的参数。……
# 将源代码添加到此项目的可执行文件。
add_executable (CMakeProject1 "CMakeProject1.cpp" "CMakeProject1.h")
……
远程Linux地址以及CMakeLists.txt文件,开始调试。
