背景: 公司要用vs code 在linux下跑通代碼單步調試。下面是具體的操作。
安裝環境:ubuntu 18.04, 系統自帶的gcc 7.3.0
下載地址:https://code.visualstudio.com/
點擊下載 .deb格式的文件
不需要修改,選擇open with software install(default),點擊ok。下載完成後,點擊install,就可以安裝了。
安裝完成後,可以看到/usr/bin下有一個code可執行文件,直接在命令行輸入code,就可以打開圖像編輯界面。
按ctrl+p打開快速命令框,輸入ext install cpptools
點擊install安裝插件,
安裝完成後,需要點擊reload,使安裝生效。Reload之後的效果:
建議安裝的插件:
c/c++
code runner
c/c++ compile run
c/c++ snippets
epitech c/c++
c/c++ advanced lint
設置code runner調試環境
code runner的修改步驟:
點擊vs code的工具欄:File-->preferences-->settings-->輸入 code runner-->edit in settings.json
默認是不能編輯的,需要點擊左側的修改圖標(鉛筆圖標),在右側出現的user settings中進行修改。
如下圖,左側是默認的,右側是用戶配置的。
還需要設置2個勾選項,搜索相應的配置項,勾選即可:
"code-runner.runInTerminal": true,
"code-runner.ignoreSelection": true,
編寫代碼
自己建一個文件夾,路徑最好是全英文的。使用vs code打開文件夾:
File-->open folder-->選擇你的文件夾,打開,工作區域如下所示:
編寫一段helloworld代碼,命名爲hello.cpp:
#include <stdio.h>
//#include <stdlib.h>
//#include <pthread.h>
int main()
{
printf("hello world !\n");
//system("pause");
int i = 0;
i += 10;
printf("this is the begin of the world!\n");
return 0;
}
編寫完代碼,可以配置調試相關環境和具體的構建命令了。
修改配置文件
點擊左側第4個按鈕,打開了調試界面。再點擊工具欄帶紅點的齒輪按鈕,選擇C++(GDB/LLDB),會自動生成一個Launch.json.
需要修改launch.json配置文件爲如下:
工具欄 termital à configure default build task à create tasks.json file from template à select a task template à others
需要將自動生成的Tasks.json修改爲如下:
使當前編輯的文件爲main()所在的.cpp文件,按F5,就可以看到運行的結果了。如需要調試,可以在需要的位置打斷點進行調試。
設置code runner調試環境
code runner的修改步驟:
點擊vs code的工具欄:Fileàpreferencesàsettingsà輸入 code runneràedit in settings.json
如果不能編輯,點擊左側的修改圖標(鉛筆圖標),在右側出現的user settings中進行修改。
如下圖,左側是默認的,右側是用戶配置的。
還需要設置2個勾選項,搜索相應的配置項,勾選即可:
"code-runner.runInTerminal": true,
"code-runner.ignoreSelection": true,
Code runner 運行效果:
使當前編輯的文件爲main()所在的.cpp文件,右鍵-Run Code 或者按Ctrl+Alt+N,就可以看到運行的結果了。
可能會遇到的問題:
linux vs code F5後,Stopping due to fatal error: NullReferenceException: Object reference not set to an instance of an object
解決方案:
Launch.json 下"externalConsole": false
快捷鍵
- Continue / Pause F5
- Step Over F10
- Step Into F11
- Step Out Shift+F11
- Restart Ctrl+Shift+F5
- Stop Shift+F5
Shift+Tab: 對齊縮進
Ctrl+Shift+D: debug view
Ctrl+Shift+E: File Explorer view
Ctrl+space 提示可用參數
參考鏈接:
https://blog.csdn.net/zhouyingge1104/article/details/80669995
https://www.cnblogs.com/lidabo/p/5888997.html
https://code.visualstudio.com/docs/editor/variables-reference
https://code.visualstudio.com/docs/editor/tasks-appendix
https://blog.csdn.net/qq_31359295/article/details/75113078