ubuntu下的GDB的基本使用及CMake设置调试

ubuntu下的GDB基本使用

GDB是一个由GNU开源组织发布的、UNIX/LINUX操作系统下的、基于命令行的、功能强大的程序调试工具。可以用来调试C,C++程序。

GDB的安装

# 安装
sudo apt-get install gdb

查看dgb版本号

# 查看版本
gdb --version
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".

GDB的基本使用

创建一个cpp测试文件

#include <iostream>
#include <stdio.h>
void ShowRevertNum(int iNum) {
    
    
	while (iNum > 10)
        {
    
    
 				printf("%d", iNum % 10);
                iNum = iNum / 10;
        }
    printf("%d\n", iNum);
}

int main() {
    
    
    std::cout<<"Hello world!"<<std::endl;
    
    int iNum;
    printf("Please input a number :");
	scanf("%d", &iNum);
	printf("After revert : ");
	ShowRevertNum(iNum);
	
    return 0;
}

编译cpp

g++ -o myHello -g ./main.cpp

使用gdb执行调试

gdb myHello
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from myHello...done.
(gdb) 

执行后,控制台的光标停在(gdb)最后,此时可以开始进入交互调试:

基本调试命令如下
命令功能 命令 命令全称
加载一个执行程序 file <filename> -
查看代码 ll <行号> list <行号>
设置断点 b <行号> break <行号>
开始运行 r run
执行下一个断点: c continue
下一步 n next
查看变量 p <变量名> print <变量名>
查看类型 whatis <变量名> -
删除断点 d <行号> delete <行号>
清除所有断点 clear -
退出调试 quit -

CMake配置DGB调试

如何通过CMakeLists.txt设置

SET(CMAKE_BUILD_TYPE "Debug") 
# 添加对gdb的支持
SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g2 -ggdb")
SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")

设置Debug模式,CMAKE_BUILD_TYPE 可通过在执行cmake是命令时指定,不用在CMakeLists.txt中写死,如下:
cmake .. -DCMAKE_BUILD_TYPE=Debug

也可以使用 add_compile_options 命令,与设置 CMAKE_CXX_FLAGS 或 CMAKE_C_FLAGS 效果一致;
add_compile_options 是针对所有编译器的;

相关选项参数的功能解释

  • -w -W -Wall
-w 关闭编译警告。平时编写c/c++代码如果不规范,编译的时候会抛出很多警告。但是一般的警告都是可以忽略的,比如类型转换。编译的时候可以加-w关闭警告

-W 也是关闭编译警告,但是比-w智能一些,它只会显示编辑器认为会出错的警告

-Wall, 显示所有警告。
  • -g -g3
gcc 支持4中级别的调试信息

-g0表示不生成调试信息

-g3表示生成最多的调试信息

-g默认为-g2。

一般的调试信息包括行号,函数,外部变量。-g3包含其他额外的调试信息,比如宏定义。
  • -O0 -O1 -O2 -O3 -Os
-O系列选项主要用于优化代码。

-O0 不优化

-O和-O1是等价的,不影响编译速度,并且会采用一些优化算法,降低代码大小并提高代码运行速度。

-O2,会降低编译速度,但是除了包含-O1的优化算法之外,还会采用一些其他的优化算法来提高代码运行速度。

-O3,除了包含-O2所有的优化外,会采取一些向量化算法,提高代码的并行执行程度,使之更充分地利用现代cpu的流水线和cache。

-Os,-O3即使是增加代码的大小,也要提高运行速度。而这个选项在-O2的基础上,尽量减少目标的大小,这个经常用于存储量比较小的设备。

猜你喜欢

转载自blog.csdn.net/youlinhuanyan/article/details/126085714