GDB概述
GDB是个开源的命令行调试工具,它主要可以完成,启动程序、打断点、动态改变执行变量。
DEMO
第一个例子是单独调试一个文件的程序 HelloWorld.cpp
命令如下:
g++ -g -o HelloWorld HelloWorld.cppgdb HelloWorld
执行效果如下图:
下面再来看看多个文件,通过makefile make后的调式方法
首先贴上cpp的代码
Link.h
class Link { public: Link(Link* pNext, int id) : _pNext(pNext),_id(id){} Link * Next() const {return _pNext;} int Id() const {return _id;} private: int _id; Link *_pNext; }; class List { public: List(): _pHead(0){} ~List(); void Add(int id); bool Find(int id) const; Link const * GetHead() const { return _pHead; } private: Link * _pHead; };
List.cpp#include"Link.h" #include<iostream> void List::Add(int id) { //添加在列头 Link * pLink = new Link(_pHead, id); _pHead = pLink; } List::~List() { while(_pHead!=0) { Link * pLinkTmp = _pHead; _pHead = _pHead->Next(); delete pLinkTmp; } } bool List :: Find(int id) const { for(Link const * pLink = GetHead(); pLink !=0; pLink = pLink->Next()) { if(pLink->Id() == id) return true; } return false; } int main() { List list; list.Add(2); list.Add(3); std::cout << list.Find(44) << std::endl; }
makefile
objects = List.o edit:$(objects) g++ -o edit $(objects) List.o:List.cpp Link.h g++ -g -c List.cpp clean : rm edit $(objects)
总结:在编译是必须加上 -g选项,否则使用gdb时会产生Reading symbols from /mnt/dir/cpp/Link/edit...(no debugging symbols found)...done