背景: 公司要用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++檔案添加頭部(包括作者、建立和修改日期等),并為.h頭檔案添加防重複的宏
c/c++ advanced lint :靜态檢查工具擴充
編寫代碼
自己建一個檔案夾,路徑最好是全英文的。使用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 end of the world!\n");
return 0;
}
編寫完代碼,可以配置調試相關環境和具體的建構指令了。
修改配置檔案
點選左側第4個按鈕,打開了調試界面。再點選工具欄帶紅點的齒輪按鈕,選擇C++(GDB/LLDB),會自動生成一個Launch.json.
需要修改launch.json配置檔案為如下:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/bin/${fileBasenameNoExtension}.debug.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "hello_",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
工具欄 termital --> configure default build task --> create tasks.json file from template --> select a task template --> others
需要将自動生成的Tasks.json修改為如下:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "hello_",
"type": "shell",
"command": "gcc -g ${file} -o ${workspaceFolder}/bin/${fileBasenameNoExtension}.debug.out",
"problemMatcher": {
"owner": "cpp",
"fileLocation": ["relative", "${workspaceFolder}"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
},
"presentation": {
"reveal": "always",
"panel": "new"
} // display output
}
]
}
設定code runner調試環境
code runner的修改步驟:
點選vs code的工具欄:File-->preferences-->settings-->輸入 code runner-->edit in settings.json
預設是不能編輯的,需要點選左側的修改圖示(鉛筆圖示),在右側出現的user settings中進行修改。
如下圖,左側是預設的,右側是使用者配置的。
在右側中修改标紅的2行即可。這裡設定了編譯的結果放在代碼路徑的bin目錄下,是以,需要在代碼路徑下手動建立一個bin檔案夾。
"c": "cd $dir && gcc $fileName -o bin/$fileNameWithoutExt.out && cd bin && ./$fileNameWithoutExt.out",
"cpp": "cd $dir && g++ $fileName -o bin/$fileNameWithoutExt.out && cd bin && ./$fileNameWithoutExt.out",
還需要設定2個勾選項,搜尋相應的配置項,勾選即可:
"code-runner.runInTerminal": true,
"code-runner.ignoreSelection": true,
運作效果:
使目前編輯的檔案為main()所在的.cpp檔案,按F5,就可以看到運作的結果了。如需要調試,可以在需要的位置打斷點進行調試。
右鍵-Run Code 或者按Ctrl+Alt+N,可以看到code runner運作的結果。
可能會遇到的問題:
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
https://blog.csdn.net/qq_37968132/article/details/79685646
https://cutecoot.iteye.com/blog/1992595
https://github.com/Microsoft/vscode-cpptools/issues/2922