文章目錄
使用vs code 調試c++
在linux系統上調試簡單項目
如果我們僅僅是寫少量的cpp文件,不包含第三方包,可以直接使用g++ 編譯.
以下是官網的方法.
前提,安裝插件c/c++
1. 設置編譯器路徑
按ctrl+shift+p
打開命令面板
.
如上圖所示,選擇c/c++:edit configurations(UI)
設置
Compiler path
/usr/bin/g++IntelliSense mode
gcc-x64
設置完成後自動生成.vscode\c_cpp_properties.json
文件
文件內容如下
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/g++",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
在 vscode中將鼠標放在name上,可以顯示其註釋
2. 創建構建任務(build task)
按ctrl+shift+p
打開命令面板
. 輸入Tasks: Configure Default Build Task.
選擇使用模版創建task.json文件
> 選擇others
自動生成的task.json
文件內容 如下:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "echo",
"type": "shell",
"command": "echo Hello"
}
]
}
將其修改爲:
{
"version": "2.0.0",
"tasks": [
{
"label": "helloworld",
"type": "shell",
"command": "g++",
"args": [
"-g",
"-std=c++11",
"-o",
"./helloworld.out",
"helloworld.cpp"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
參數中的
-g
表示啓用調試模式編譯, 否則無法調試
3. 配置調試選項
在調試面板中選擇添加配置
,然後選擇c/c++(GDB/LLDB)
最後會生成launch.json
文件,內容如下:
{
// 使用 IntelliSense 瞭解相關屬性。
// 懸停以查看現有屬性的描述。
// 欲瞭解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "enter program name, for example ${workspaceFolder}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
需要稍作修改:
{
// 使用 IntelliSense 瞭解相關屬性。
// 懸停以查看現有屬性的描述。
// 欲瞭解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/helloworld.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
4. 添加源代碼
在項目目錄中新建helloworld.cpp
文件,寫入如下內容:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> msg {"Hello", "C++", "World", "from", "VS Code!", "and the C++ extension!"};
for (const string& word : msg)
{
cout << word << " ";
}
cout << endl;
}
設置點斷,點擊調試:
一直點擊單步跳過執行完畢後會彈出提示
無法打開“libc-start.c”: 無法讀取文件(Error: 找不到文件(/build/glibc-LK5gWL/glibc-2.23/csu/libc-start.c))。
不知到爲何會有該問題,可能是bug
小結
爲了 能夠調試,我們需要在.vscode
下建立三個文件:
c_cpp_properties.json
: 該文件是c/c++ 插件
進行代碼提示和糾錯的設置,與編譯無關.tasks.json
:該文件是設置編譯的配置文件,command
參數,設置編譯器,args
參數,設置編譯器參數. 按按Ctrl + Shift + B
鍵進行編譯.launch.json
: 調試器的配置文件,program
設置要調試的程序,MIMode
設置調試器.
在linux系統上調試複雜項目
如果要調試的項目比較複雜,使用g++編譯將會費事, 所以可以使用cmake
進行項目構建.
1. 安裝插件
Cmake
插件: 可以對cmakellists的編寫,進行代碼提示和糾錯.CMake Tools
插件: 提供一些cmake快捷命令
插件安裝完成後.打開命令面板,輸入: Cmake: quick start
,輸入項目名稱,即可新建CMakeLists.txt
文件. 其內容如下:
cmake_minimum_required(VERSION 3.0.0)
project(helloword VERSION 0.1.0)
# 手動添加的,否則,在ubuntu16.04中不會使用c++11
add_definitions(-std=c++11)
include(CTest)
enable_testing()
add_executable(helloword main.cpp)
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
將add_executable(helloword main.cpp)
改爲: add_executable(helloword helloworld.cpp)
手動添加一條命令,使編譯器使用c++11標準
add_definitions(-std=c++11)
2. 修改launch.json文件
在這裏, tasks.json
文件作廢,因爲我們不在使用tasks.json
的配置文件進行編譯.
修改launch.json
文件, 修改program
參數指定的可執行文件路徑
{
// 使用 IntelliSense 瞭解相關屬性。
// 懸停以查看現有屬性的描述。
// 欲瞭解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/helloworld",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
3. 在cmake控制面板上依次點擊configure
和build
.
4. 調試
現在可以在調試面板上進行調試操作了.
小結
- 安裝插件
cmake
和cmake tools
- 捨棄
tasks.json
文件, 修改launch.json
文件 - 使用cmake構建項目
- 調試