CMakeLists.txt编译进行调试的GDB简介

任务描述:利用CMakeLists.txt设置生成支持调试的.gdb文件,进而可以对代码进行调试。

Step 1. 设置gdb指令

SET(CMAKE_BUILD_TYPE "Debug")
SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g2 -ggdb")
SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")

Step 2. 编译运行调试

$ cmake ..
$ make
$ gdb ./demo

具体的编译运行指令含义和完整的CMakeLists.txt文件,请参考Cmake编译可执行文件。

Step 3. 调试指令

进入gdb调试模式后如下图

$ gdb ./demo
GNU gdb (Ubuntu 8.1-0ubuntu3.2) 8.1.0.20180409-git
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
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:
.
Find the GDB manual and other documentation resources online at:
.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./demo...done.
(gdb) 

此时输入list指令可以看到打印出全部代码,代码过长键入回车键可以继续输出,直到全部显示出来。

(gdb) list
1	#include 
2	#include "a/a.h"
3	#include 
4	
5	#define NUM 100
6	
7	int main()
8	{
9		cv::Mat tt;
10		cv::imshow("", tt);
(gdb) 
11		cv::waitKey(0);
12		std::cout << NUM << std::endl;
13		
14		std::cout << "Hello World!" << std::endl;
15	
16	    a();
17	
18		cv::Mat img = cv::imread("/home/sz/Desktop/test/test.jpeg");
19		cv::imshow("Src", img);
20		cv::waitKey(0);
(gdb) 
21		cv::destroyAllWindows();
22	
23		return 1;
24	}(gdb) 
Line number 25 out of range; /home/sz/Desktop/test/test.cpp has 24 lines.
(gdb) 

上图就是我们的示例代码。

输入start指令开始调试。

(gdb) start
Temporary breakpoint 1 at 0x83b6: file /home/sz/Desktop/test/test.cpp, line 8.
Starting program: /home/sz/Desktop/test/build/demo 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Temporary breakpoint 1, main () at /home/sz/Desktop/test/test.cpp:8
8	{
(gdb) 

其他指令如下

逐行调试:
n

进入函数调试:
s

查看变量数据
p 变量名

退出调试:
q

 

参考链接:参考,感谢博主分享。

你可能感兴趣的:(Ubuntu,C/C++)