vscode C++遠程調試運行(學習C++用)
目標:
連接遠程主機 (ssh)
配置C++編譯環(huán)境 (輸出結果后刪除二進制文件)

步驟:
安裝Remote SSH,連接遠程主機
Visual Studio 官方文檔
https://code.visualstudio.com/docs/remote/ssh
圖標
2. 配置C++編譯運行環(huán)境
主要參考下面兩篇文檔
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 創(chuàng)建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 創(chuàng)建launch.json用于調試運行
在菜單欄選擇Debug>Add Configuration, 選擇C++ (GDB/LLDB), 在下拉欄中選擇g++ build and debug active file.

這會創(chuàng)建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++遠程調試運行內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- Linux搭建C++開發(fā)調試環(huán)境的方法步驟
- c++代碼調試方式的幾點建議
- 解決vscode下調試c/c++程序一閃而過的問題(Windows)
- vscode配置遠程開發(fā)環(huán)境并遠程調試運行C++代碼的教程
- VSCode遠程開發(fā)調試服務器c/c++代碼
- ubunt18.04LTS+vscode+anaconda3下的python+C++調試方法
- C++運算符重載實例代碼詳解(調試環(huán)境 Visual Studio 2019)
- 詳解AndroidStudio3.0開發(fā)調試安卓NDK的C++代碼
- C++調試記錄與心得分享
- 詳解C++的反調試技術與繞過手法
相關文章
Windows下搭建FFmpeg開發(fā)調試環(huán)境的詳細步驟
這篇文章主要介紹了Windows下搭建FFmpeg開發(fā)調試環(huán)境,本文以VS2017為例一步步介紹怎么搭建一個可供單步調試的FFmpeg項目,需要的朋友可以參考下2022-07-07
C++中fstream,ifstream及ofstream用法淺析
這篇文章主要介紹了C++中fstream,ifstream及ofstream用法,適合C++初學者學習文件流的操作,需要的朋友可以參考下2014-08-08

