亲宝软件园·资讯

展开

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

兰彻斯特 人气:0

目标:

连接远程主机 (ssh)
配置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文件。

按需修改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.

这会创建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

总结

加载全部内容

相关教程
猜你喜欢
用户评论