C/C++常见的几种Debug总结
VScode单个文件直接debug
在调试C/C++代码时,debug是快速定位到问题的重要方式,因此总结一下。
必不可少的几个插件
- C/C++
- C/C++ Extension Pack
- CMake
- Bazel
步骤
1.新建单个脚本文件并添加断点
2. 按F5选择
3.此时会进入断点处,即可进行debug,同时可以选择"create launch json"
debug时会自动生成tasks.json到.vscode目录下,具体如下所示。
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
cmake 多文件debug
创建原文件目录
新建头文件目录 inc和源文件目录src 以及生成文件目录build
如下图所示:
新建cmake文件
在项目根目录下新建CMakeLists.txt
其中主要需要设置* set(CMAKE_BUILD_TYPE DEBUG)*才可以
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
CMAKE_POLICY(VERSION 2.6)
message("start building...")
#debug版本才能调试
set(CMAKE_BUILD_TYPE DEBUG)
project(main)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_COMPILER g++)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/inc)
file(GLOB_RECURSE PROJECT_HEADERS
"inc/*.h"
)
file(GLOB_RECURSE PROJECT_SOURCES
"src/*.cc"
"src/*.cpp"
)
add_executable(${PROJECT_NAME} ${PROJECT_HEADERS} ${PROJECT_SOURCES})
message(${PROJECT_NAME} " building end!")
新建setup.sh
具体如下:
if [ ! -d "./build/" ];then
mkdir ./build
else
echo "文件夹已经存在"
rm -rf ./build/*
fi
cd build/
rm -rf *
cmake ..
make
echo "build over!"
echo `date`
配置launch.json
由于F5需要调用生成bin文件,需要在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}/build/main",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}
编译并添加断点进行debug
一键运行setup.sh即可编译文件,编译成功后,按F5即可debug
bazel 多文件debug
建立工作目录
建立lib、main,文件夹如下图所示,同时在根目录下新建一个WORKSPACE文件
配置json
其中hello-world是生成bin文件名称
- 其中tasks.json文件如下所示:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Build Example (Debug)",
"type": "shell",
"command": "bazel build //main:hello-world -c dbg",
"windows": {
"command": "bazel build :hello-world --enable_runfiles -c dbg"
},
"osx": {
"command": "bazel build :hello-world -c dbg --spawn_strategy=standalone",
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
- 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": "BazelBuild",
"preLaunchTask": "Build Example (Debug)",
"type": "cppdbg",
"MIMode": "gdb",
"request": "launch",
"program": "${workspaceFolder}/bazel-bin/main/hello-world",
// "program": "${workspaceFolder}/bazel-out/k8-dbg/bin/main/hello-world",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/bazel-out/k8-dbg/bin/main/hello-world.runfiles/__main__/",
"environment": [],
"console": "externalTerminal",
}
]
}
添加断点调试
其中红框中时按f5后自动编译生成的文件夹
本文含有隐藏内容,请 开通VIP 后查看