如何使用VSCode进行C语言的远程调试与运行?

2026-04-29 11:382阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计667个文字,预计阅读时间需要3分钟。

如何使用VSCode进行C语言的远程调试与运行?

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

步骤:

1.安装Remote SSH

2.连接远程主机

3.下载并安装Visual Studio官方文档

网址:https://code.visualstudio.com/docs/remote/ssh 图标:2. 配置

目标:

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

步骤:

安装Remote SSH,连接远程主机

Visual Studio 官方文档

code.visualstudio.com/docs/remote/ssh

图标

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

主要参考下面两篇文档

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

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.

如何使用VSCode进行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++远程调试运行内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

本文共计667个文字,预计阅读时间需要3分钟。

如何使用VSCode进行C语言的远程调试与运行?

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

步骤:

1.安装Remote SSH

2.连接远程主机

3.下载并安装Visual Studio官方文档

网址:https://code.visualstudio.com/docs/remote/ssh 图标:2. 配置

目标:

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

步骤:

安装Remote SSH,连接远程主机

Visual Studio 官方文档

code.visualstudio.com/docs/remote/ssh

图标

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

主要参考下面两篇文档

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

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.

如何使用VSCode进行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++远程调试运行内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!