阅读量:2
在Ubuntu上进行C++开发时,可以使用多种调试工具,其中最常用的是**GNU调试器(GDB)和Visual Studio Code(VS Code)**搭配其扩展。以下是详细的使用步骤:
使用GDB进行调试
- 安装GDB:
sudo apt-get update
sudo apt-get install gdb
- 编译代码:
在编译时添加-g标志以包含调试信息。
g++ -g -o my_program my_program.cpp
- 启动GDB:
gdb my_program
- 设置断点:
break main
- 运行程序:
run
- 调试命令:
next:单步跳过函数调用step:单步进入函数continue:继续执行程序直到遇到下一个断点print variable_name:打印变量的值backtrace:显示当前调用堆栈
使用Visual Studio Code进行调试
- 安装VS Code和C/C++扩展:
下载并安装Visual Studio Code,然后打开扩展市场,搜索并安装“C/C++”扩展。
- 配置调试环境:
- 创建
.vscode文件夹,并在其中创建tasks.json和launch.json文件。
- tasks.json:
用于配置编译任务。
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
- launch.json:
用于配置调试任务。
{
"version": "0.2.0",
"configurations": [
{
"name": "g++",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"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"
}
]
}
- 启动调试:
按F5键或点击工具栏上的绿色调试箭头图标启动调试会话。
通过上述步骤,你可以在Ubuntu上使用GDB或VS Code进行C++程序的调试。这些工具提供了强大的功能,如设置断点、单步执行、查看变量值等,帮助你有效地定位和修复程序中的错误。
以上就是关于“Ubuntu C++开发中如何使用调试工具”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm