温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

vscode C++远程调试运行(学习C++用)

发布时间:2020-09-21 21:29:19 来源:脚本之家 阅读:291 作者:兰彻斯特 栏目:编程语言

目标:

连接远程主机 (ssh)
配置C++编译环境 (输出结果后删除二进制文件)

vscode C++远程调试运行(学习C++用)

步骤:

安装Remote SSH,连接远程主机

Visual Studio 官方文档

https://code.visualstudio.com/docs/remote/ssh

图标

2. 配置C++编译运行环境

主要参考下面两篇文档

https://code.visualstudio.com/docs/cpp/config-wsl

https://code.visualstudio.com/docs/editor/tasks

2.1 新建一个C++源文件HelloWorld.cpp(测试用)

#include <iostream>

int main(){
 std::cout<<"Hello World!\n";
 return 0;
}

2.2 安装 Microsoft C/C++插件

注意安装到远程主机上

2.3 创建tasks.json文件

从菜单栏选择Terminal>Configure Default Build Task, 在下拉栏里选择C/C++: g++ build active file. 这会生成tasks.json文件。

vscode C++远程调试运行(学习C++用)

按需修改tasks.json文件:

{
 "tasks": [
 {
  //编译源文件
  "type": "shell",
  "label": "g++ build active file",
  "command": "/usr/bin/g++",
  "args": [
  "-std=c++11", //C++版本, 可不加
  "-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", //删除过程不切换终端(专注程序输出)
  }
 }
 ],
 "version": "2.0.0"
}

2.4 创建launch.json用于调试运行

在菜单栏选择Debug>Add Configuration, 选择C++ (GDB/LLDB), 在下拉栏中选择g++ build and debug active file.

vscode C++远程调试运行(学习C++用)

这会创建launch.json, 编辑如下

{
 "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": "Enable pretty-printing for gdb",
  "text": "-enable-pretty-printing",
  "ignoreFailures": true
  }
 ],
 "preLaunchTask": "g++ build active file",
 "postDebugTask": "delete output file",
 "miDebuggerPath": "/usr/bin/gdb"
 }
 ]
 }

注:这里“preLaunchTask”调用tasks.json文件里定义的“g++ build and debug active file”任务, “postDebugTask”调用“delete output file”任务用来在程序运行结束后删除二进制文件。

2.5 调试F5, 不调试直接运行Cltr+F5

总结

到此这篇关于vscode C++远程调试运行(学习C++用)的文章就介绍到这了,更多相关vscode C++远程调试运行内容请搜索亿速云以前的文章或继续浏览下面的相关文章希望大家以后多多支持亿速云!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI