前言
使用vscode配置c++ debug環境的好處
1、可以借助vscode方便輕量的擴展和功能
2、避免了傳統使用gdb 復雜按鍵以及不夠直觀的可視化
3、方便一次運行,斷點處查看變量,降低找bug難度
4、某大公司項目采用類似配置,經過實踐檢驗
配置c++運行環境
MSVC在Windows下編譯c/c++
**gcc, g++**分別是GNU的c & c++編譯器,在Linux下面用。
cmake的輸入是CMakeLists.txt(描述編譯過程),輸出是makefile。build過程的輸入是makefile,輸出結果是可執行文件,build的過程會調用編譯器和連接器來完成整個過程。
qmake用來build qt工程。
MINGW包含gcc和一系列工具,是windows下的gnu環境,讓開發者在windows下可以寫gnu的c/c++代碼, 編譯的結果是windows的可執行文件exe,PE格式的,在windows下運行。
可以自行搜索不同環境的配置方式
配置VSCODE
下載擴展
配置項目
首先我們創建使用Vscode打開項目工程區
然后在該工程下創建如下文件
也就是說當調試一個多文件c++項目時,將使用launch.json進行調試,其依賴于preLaunchTask定義的build任務。
這個任務通過cmake 和make進行構建。參考鏈接1給出了更加詳細的介紹
而如果只是簡單調試單文件可以略去task.json的配置,參考鏈接2給出了更加詳細的介紹
launch.json
{"version": "0.2.0","configurations": [{"name": "g++ - Build and debug active file","type": "cppdbg","request": "launch","program": "${workspaceFolder}/build/xxxxxx","args": [],"stopAtEntry": false,"cwd": "${workspaceFolder}","environment": [],"externalConsole": false,"MIMode": "gdb","setupCommands": [{"description": "Enable pretty-printing for gdb","text": "-enable-pretty-printing","ignoreFailures": true}],"preLaunchTask": "build","miDebuggerPath": "/usr/bin/gdb"}]
}
tasks.json
{"version": "2.0.0","tasks": [{"label": "cmake","type": "shell","command": "cmake","args": ["-B","build"],"options": {"cwd": "${workspaceFolder}"}},{"label": "make","type": "shell","command": "cmake","args": ["--build","build"],"options": {"cwd": "${workspaceFolder}"}},{"label": "build","dependsOn": ["cmake","make"]}],
}
最后一鍵F5 即可進行斷點調試了
參考資料
https://zhuanlan.zhihu.com/p/618043511
https://blog.csdn.net/qq_42417071/article/details/137438374