配置详解:vscode中远程调试c++

 3280

配置详解:vscode中远程调试c++


最近在学习linux webserver开发,需要在linux下调试自己的C/C++代码,但是linux下不像在windows下,直接Visio Studio或者其它集成开发环境那么方便,现在的linux下开发,比较麻烦。于是可以考虑使用VScode远程开发。但是网上的很多教程都不是很清晰,这里在尝试了很多教程后,踩了不少坑,最后总结如下。

1.系统配置

远程系统:ubuntu18.04(虚拟机)
开发主机:windows10

2.ubuntu远程端安装软件和设置

(1)安装必要软件:ssh(系统通信),gdb,gsdbserver(代码调试):

  1. sudo apt-get install openssh-server
  2. sudo apt-get install gdb
  3. sudo apt-get install gdbserver

(2)创建测试文件夹和文件

注意:

虽然你可能想一步到位,直接拿自己最后的程序测试,但是这里不建议这么做,建议先新建一个hello,world程序测试,成功后再调试自己的代码。

文件夹位置和内容无所谓,但是最好简单一些

  1. cd ~/桌面
  2. mkdir testvs
  3. cd testvs
  4. touch main.cpp
  5. gedit main.cpp

其中main.cpp代码为:

  1. #include <stdio.h>
  2.   
  3. int main()
  4. {
  5.     int a = 1;
  6.     printf("hello world\n");
  7.     getchar();
  8.     return 0;
  9. }

(3)编译,得到可执行文件

g++ main.cpp -o main -g

注意:

加-g选项,不然没法用gdb调试

运行后testvs文件夹下有main.cpp和main两个文件

(4)启动gdbserver

(4.1)首先看一下自己的ubuntu系统ip地址:

hostname -I


配置详解:vscode中远程调试c++


可以得到本地ip地址为192.168.199.131

(4.2)启动gdbserver(注意更改ip地址和测试文件目录)

gdbserver 192.168.199.131:2000 ~/桌面/testvs/main


配置详解:vscode中远程调试c++


3.主机VScode设置

(1)首先在VScode中安装下面几个插件:

C/C++

C/C++ Extension Pack

Remote - SSH

Remote Development

(2)ssh远程连接

左下角“管理”->"控制面板",之后找到选项“Remote-SSH:Connect to Host...” -> Add New SSH Host...

输入ubuntu系统ip地址,出来新界面


配置详解:vscode中远程调试c++


红框内输入ubuntu系统密码,左下角显示绿色ip地址即连接成功,如下图。


配置详解:vscode中远程调试c++


(3)打开测试文件

打开文件夹 -> 选择测试文件夹目录,点“确定”按钮


配置详解:vscode中远程调试c++


选中C/C++扩展,“在SSH:XXX中安装”。C/C++ Extension Pack扩展同理
然后重启Vscode和Ubuntu中的gdbserver(一定得要重启,否则接下来的步骤会报错)重新执行上述远程连接流程。

(4)设置配置文件

(4.1)配置tasks.json

从菜单栏选择Terminal>Configure Default Build Task, 在下拉栏里选择C/C++: g++ build active file. 之后生成tasks.json文件,将内容更换为:

  1. {
  2.     // 有关 tasks.json 格式的文档,请参见
  3.     // https://go.microsoft.com/fwlink/?LinkId=733558
  4.     "version": "2.0.0",
  5.     "tasks": [
  6.         {
  7.             "type": "shell",
  8.             "label": "g++ build active file",
  9.             "command": "/usr/bin/g++",
  10.             "args": [
  11.                 "-std=c++11",
  12.                 "-g",
  13.                 "${file}",
  14.                 "-o",
  15.                 "${fileDirname}/${fileBasenameNoExtension}"
  16.             ],
  17.             "options": {
  18.                 "cwd": "/usr/bin"
  19.             },
  20.             "problemMatcher": [
  21.                 "$gcc"
  22.             ],
  23.             "group": {
  24.                 "kind": "build",
  25.                 "isDefault": true
  26.             }
  27.         },
  28.         { //删除二进制文件
  29.             "type": "shell",
  30.             "label": "delete output file",
  31.             "command": "rm",
  32.             "args": [
  33.                 "${fileDirname}/${fileBasenameNoExtension}"
  34.             ],
  35.             "presentation": {
  36.                 "reveal": "silent", //删除过程不切换终端(专注程序输出)
  37.             }
  38.         }
  39.     ]
  40. }

(4.2)配置launch.json

在菜单栏选择Debug>Add Configuration, 选择C++ (GDB/LLDB), 在下拉栏中选择g++ build and debug active file.生成launch.json,内容更改为:

  1. {
  2.     // 使用 IntelliSense 了解相关属性。 
  3.     // 悬停以查看现有属性的描述。
  4.     // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
  5.     "version": "0.2.0",
  6.     "configurations": [
  7.         {
  8.             "name": "g++ build and debug active file",
  9.             "type": "cppdbg",
  10.             "request": "launch",
  11.             "program": "${fileDirname}/${fileBasenameNoExtension}",
  12.             "args": [],
  13.             "stopAtEntry": false,
  14.             "cwd": "${workspaceFolder}",
  15.             "environment": [],
  16.             "externalConsole": false,
  17.             "MIMode": "gdb",
  18.             "setupCommands": [
  19.                 {
  20.                      "description": "为 gdb 启用整齐打印",
  21.                      "text": "-enable-pretty-printing",
  22.                      "ignoreFailures": true
  23.                 }
  24.             ],
  25.             "preLaunchTask": "g++ build active file",
  26.             "postDebugTask": "delete output file",
  27.             "miDebuggerPath": "/usr/bin/gdb"
  28.         }
  29.     ]
  30. }


4.运行调试

在main.cpp下调试运行即可


配置详解:vscode中远程调试c++

TAG标签:
本文网址:https://www.zztuku.com/index.php/detail-13193.html
站长图库 - 配置详解:vscode中远程调试c++
申明:本文转载于《博客园》,如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐