php入門到就業(yè)線上直播課:進入學(xué)習(xí)
Apipost = Postman + Swagger + Mock + Jmeter 超好用的API調(diào)試工具:點擊使用
最近在學(xué)習(xí)linux webserver開發(fā),需要在linux下調(diào)試自己的C/C++代碼,但是linux下不像在windows下,直接Visio Studio或者其它集成開發(fā)環(huán)境那么方便,現(xiàn)在的linux下開發(fā),比較麻煩。于是可以考慮使用VScode遠程開發(fā)。但是網(wǎng)上的很多教程都不是很清晰,這里在嘗試了很多教程后,踩了不少坑,最后總結(jié)如下?!就扑]學(xué)習(xí):《vscode入門教程》】
1.系統(tǒng)配置
遠程系統(tǒng):ubuntu18.04(虛擬機)
開發(fā)主機:windows10
2.ubuntu遠程端安裝軟件和設(shè)置
(1)安裝必要軟件:ssh(系統(tǒng)通信),gdb,gsdbserver(代碼調(diào)試):
sudo apt-get install openssh-server sudo apt-get install gdb sudo apt-get install gdbserver
(2)創(chuàng)建測試文件夾和文件
注意:
- 雖然你可能想一步到位,直接拿自己最后的程序測試,但是這里不建議這么做,建議先新建一個hello,world程序測試,成功后再調(diào)試自己的代碼。
- 文件夾位置和內(nèi)容無所謂,但是最好簡單一些
cd ~/桌面 mkdir testvs cd testvs touch main.cpp gedit main.cpp
其中main.cpp代碼為:
#include <stdio.h> int main() { int a = 1; printf("hello worldn"); getchar(); return 0; }
(3)編譯,得到可執(zhí)行文件
g++ main.cpp -o main -g
注意:
- 加-g選項,不然沒法用gdb調(diào)試
- 運行后testvs文件夾下有main.cpp和main兩個文件
(4)啟動gdbserver
(4.1)首先看一下自己的ubuntu系統(tǒng)ip地址:
hostname -I
可以得到本地ip地址為192.168.199.131
(4.2)啟動gdbserver(注意更改ip地址和測試文件目錄)
gdbserver 192.168.199.131:2000 ~/桌面/testvs/main
3.主機VScode設(shè)置
(1)首先在VScode中安裝下面幾個插件:
- C/C++
- C/C++ Extension Pack
- Remote – SSH
- Remote Development
(2)ssh遠程連接
左下角“管理”->"控制面板",之后找到選項“Remote-SSH:Connect to Host…” -> Add New SSH Host…
輸入ubuntu系統(tǒng)ip地址,出來新界面
紅框內(nèi)輸入ubuntu系統(tǒng)密碼,左下角顯示綠色ip地址即連接成功,如下圖。
(3)打開測試文件
打開文件夾 -> 選擇測試文件夾目錄,點“確定”按鈕
選中C/C++擴展,“在SSH:XXX中安裝”。C/C++ Extension Pack擴展同理
然后重啟Vscode和Ubuntu中的gdbserver(一定得要重啟,否則接下來的步驟會報錯)重新執(zhí)行上述遠程連接流程。
(4)設(shè)置配置文件
(4.1)配置tasks.json
從菜單欄選擇Terminal>Configure Default Build Task, 在下拉欄里選擇C/C++: g++ build active file. 之后生成tasks.json文件,將內(nèi)容更換為:
{ // 有關(guān) tasks.json 格式的文檔,請參見 // https://go.microsoft.com/fwlink/?LinkId=733558 "version": "2.0.0", "tasks": [ { "type": "shell", "label": "g++ build active file", "command": "/usr/bin/g++", "args": [ "-std=c++11", "-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", //刪除過程不切換終端(專注程序輸出) } } ] }
(4.2)配置launch.json
在菜單欄選擇Debug>Add Configuration, 選擇C++ (GDB/LLDB), 在下拉欄中選擇g++ build and debug active file.生成launch.json,內(nèi)容更改為:
{ // 使用 IntelliSense 了解相關(guān)屬性。 // 懸停以查看現(xiàn)有屬性的描述。 // 欲了解