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 <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
總結
到此這篇關於vscode C++遠端除錯執行(學習C++用)的文章就介紹到這了,更多相關vscode C++遠端除錯執行內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!