vscode远程调试c++
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
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01