• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

vscode远程调试c++

武飞扬头像
PHP中文网
帮助52

1.系统配置

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

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

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

sudo apt-get install openssh-server
sudo apt-get install gdb
sudo apt-get install gdbserver

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

注意:

  • 虽然你可能想一步到位,直接拿自己最后的程序测试,但是这里不建议这么做,建议先新建一个hello,world程序测试,成功后再调试自己的代码。
  • 文件夹位置和内容无所谓,但是最好简单一些
cd ~/桌面
mkdir testvs
cd testvs
touch main.cpp
gedit main.cpp

其中main.cpp代码为:

#include <stdio.h>
 
int main()
{
    int a = 1;
    printf("hello world\n");
    getchar();
    return 0;
}

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

g main.cpp -o main -g
注意:

  • 加-g选项,不然没法用gdb调试
  • 运行后testvs文件夹下有main.cpp和main两个文件

(4)启动gdbserver

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

hostname -I
学新通技术网
可以得到本地ip地址为192.168.199.131

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

gdbserver 192.168.199.131:2000 ~/桌面/testvs/main
学新通技术网

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地址,出来新界面

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

学新通技术网

(3)打开测试文件

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

学新通技术网
选中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文件,将内容更换为:

{
    // 有关 tasks.json 格式的文档,请参见
     // https://go.microsoft.com/fwlink/?LinkId=733558
     "version": "2.0.0",
     "tasks": [
     {
     "type": "shell",
     "label": "g   build active file",
     "command": "/usr/bin/g  ",
     "args": [
     "-std=c  11",
     "-g",
     "${file}",
     "-o",
     "${fileDirname}/${fileBasenameNoExtension}"
     ],
     "options": {
     "cwd": "/usr/bin"
     },
     "problemMatcher": [
     "$gcc"
     ],
     "group": {
     "kind": "build",
     "isDefault": true
     }
     },
     { //删除二进制文件
     "type": "shell",
     "label": "delete output file",
     "command": "rm",
     "args": [
     "${fileDirname}/${fileBasenameNoExtension}"
     ],
     "presentation": {
     "reveal": "silent", //删除过程不切换终端(专注程序输出)
     }
     }
     ]
    }

(4.2)配置launch.json

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

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
    {
    "name": "g   build and debug active file",
    "type": "cppdbg",
    "request": "launch",
    "program": "${fileDirname}/${fileBasenameNoExtension}",
    "args": [],
    "stopAtEntry": false,
    "cwd": "${workspaceFolder}",
    "environment": [],
    "externalConsole": false,
    "MIMode": "gdb",
    "setupCommands": [
    {
     "description": "为 gdb 启用整齐打印",
     "text": "-enable-pretty-printing",
     "ignoreFailures": true
    }
    ],
    "preLaunchTask": "g   build active file",
    "postDebugTask": "delete output file",
    "miDebuggerPath": "/usr/bin/gdb"
    }
    ]
   }

4.运行调试

在main.cpp下调试运行即可

学新通技术网

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanfckbk
系列文章
更多 icon
同类精品
更多 icon
继续加载