远程编译C语言,vscode C++远程调试运行(学习C++用)

目标:

连接远程主机 (ssh)

配置C++编译环境 (输出结果后删除二进制文件)

a7217f407e0c1e875f9bf444679a50a9.png

步骤:

安装Remote SSH,连接远程主机

Visual Studio 官方文档

图标

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

主要参考下面两篇文档

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

#include

int main(){

std::cout<

return 0;

}

注意安装到远程主机上

2.3 创建tasks.json文件

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

f6bbb80dc40dd3e7d167806a4038c7fd.png

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

010ecfa311a6fea74eb50b328e0ba1ad.png

这会创建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++远程调试运行内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值