linux下gdb调试c/cpp程序
作为一个程序员,除了要写好程序,还有个必须的技能就是要学会调试程序,也就是debug,很多IDE都内置UI调试程序功能,但是有的时候,一个c/cpp项目是部署在服务器端的,只有终端环境,这个时候要想在线上去快速定位一个紧急的bug,就需要我们在服务器环境进行调试定位解决问题。同时,我们平时在写代码的时候,如果遇到一些疑难杂症,也需要我们进行调试。所以调试技能是必备的。
所以自己把学习gdb调试的过程记录下来作为回顾,以ubuntu环境为学习环境。
安装
首先要安装的就是
- gcc
- g++
- gdb
123 sudo apt install gccsudo apt install g++sudo apt install gdb
使用
编译程序
现在有个程序如下:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 #include <iostream>#include <stack>#include <queue>using namespace std;typedef int tree_node_elem_n;struct binary_tree_node_t {binary_tree_node_t *left;binary_tree_node_t *right;tree_node_elem_n elem;binary_tree_node_t(){};binary_tree_node_t(tree_node_elem_n value){elem = value;left = nullptr;right = nullptr;}};void pre_order_r(const binary_tree_node_t *root,int(*visit)(const binary_tree_node_t*)){if(root == nullptr) return;int n;n = visit(root);cout << n << " ";pre_order_r(root->left,visit);pre_order_r(root->right,visit);}int visit(const binary_tree_node_t* n){return n->elem;}void in_order_r(const binary_tree_node_t *root,int(*visit)(const binary_tree_node_t*)){if(root == nullptr) return;int n;in_order_r(root->left,visit);n = visit(root);cout << n << " ";in_order_r(root->right,visit);}void post_order_r(const binary_tree_node_t *root,int(*visit)(const binary_tree_node_t*)){if(root == nullptr) return;int n;post_order_r(root->left,visit);post_order_r(root->right,visit);n = visit(root);cout << n << " ";}void pre_order(const binary_tree_node_t *root,int(*visit)(const binary_tree_node_t*)){const binary_tree_node_t *p;stack<const binary_tree_node_t *> s;p = root;if(p != nullptr) s.push(p);// while(s)}int main(){binary_tree_node_t node1(1);binary_tree_node_t node2(4);binary_tree_node_t node3(5);binary_tree_node_t node4(1);binary_tree_node_t node5(6);binary_tree_node_t node6(3);node1.left = &node2;node1.right = &node3;node2.left = &node4;node2.right = &node5;// node4.left = nullptr;// node4.right = nullptr;node5.left = &node6;// node5.right = nullptr;// node6.left = nullptr;// node6.right = nullptr;pre_order_r(&node1,visit);cout<<endl;in_order_r(&node1,visit);cout<<endl;post_order_r(&node1,visit);cout<<endl;return 0;}
首先要编译程序,编译成有debug信息的可执行文件,命令如下