• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

c++ 搞代码 4年前 (2022-01-06) 18次浏览 已收录 0个评论

这篇文章主要介绍了vscode C++远程调试运行(学习C++用),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

目标:

连接远程主机 (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  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

总结

到此这篇关于vscode C++远程调试运行(学习C++用)的文章就介绍到这了,更多相关vscode C++远程调试运行内容请搜索gaodaima搞代码网以前的文章或继续浏览下面的来源gaodaimacom搞#^代%!码&网相关文章希望大家以后多多支持gaodaima搞代码网

以上就是vscode C++远程调试运行(学习C++用)的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:vscode C++远程调试运行(学习C++用)

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址