b 行号:打断点 info b:查看断点 d 断点编号: 取消断点 l 行号:显示代码 l main:显示包含main的那一行 r:run,开始运行程序,跳到第一个断点 s:step,逐语句,对应vs的F11(进入函数) n:next,逐过程,对应vs的F10 c:continue,跳转道下一个断点 p:查看变量 display / undisplay:常显示 或 取消常显示 until 行号:跳转到指定行 finish:执行完一个函数后停下 bt:查看函数调用堆栈
提醒:编译的时候记得加上-g选项指定debug版本
下面是一个用于演式的示例代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include<stdio.h>
intAdd(int a,int b) { printf("Add(a,b)\n"); return a+b; }
intmain() { printf("hello wolrd!\n"); int ret=Add(1,20); printf("ret: %d\n",ret); return0; }
[muxue@bt-7274:~/GIT/raspi/vim/TestGdb]$ gdb test_g GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7 Copyright (C) 2013 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-redhat-linux-gnu". For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>... Reading symbols from /home/muxue/GIT/raspi/vim/TestGdb/test_g...done. (gdb) l 2 3 int Add(int a,int b) 4 { 5 printf("Add(a,b)\n"); 6 return a+b; 7 } 8 9 int main() 10 { 11 printf("hello wolrd!\n"); (gdb) b 11 Breakpoint 1 at 0x4005a7: file test.c, line 11. (gdb) ll Undefined command: "ll". Try "help". (gdb) l 10 5 printf("Add(a,b)\n"); 6 return a+b; 7 } 8 9 int main() 10 { 11 printf("hello wolrd!\n"); 12 int ret=Add(1,20); 13 printf("ret: %d\n",ret); 14 return 0; (gdb) b 13 Breakpoint 2 at 0x4005c3: file test.c, line 13. (gdb) r Starting program: /home/muxue/GIT/raspi/vim/TestGdb/test_g
Breakpoint 1, main () at test.c:11 11 printf("hello wolrd!\n"); Missing separate debuginfos, use: debuginfo-install glibc-2.17-326.el7_9.x86_64 (gdb) s hello wolrd! 12 int ret=Add(1,20); (gdb) s Add (a=1, b=20) at test.c:5 5 printf("Add(a,b)\n"); (gdb) p ret No symbol "ret"in current context. (gdb) finish Run till exit from #0 Add (a=1, b=20) at test.c:5 Add(a,b) 0x00000000004005c0 in main () at test.c:12 12 int ret=Add(1,20); Value returned is $1 = 21 (gdb) s
Breakpoint 2, main () at test.c:13 13 printf("ret: %d\n",ret); (gdb) p ret $2 = 21 (gdb) p &ret $3 = (int *) 0x7fffffffdf3c (gdb) s ret: 21 14 return 0; (gdb) s 15 }(gdb) q A debugging session is active.
Inferior 1 [process 5932] will be killed.
Quit anyway? (y or n) y [muxue@bt-7274:~/GIT/raspi/vim/TestGdb]$
[muxue@bt-7274:~/GIT/raspi/vim/TestMake]$ ls makefile test.c [muxue@bt-7274:~/GIT/raspi/vim/TestMake]$ make gcc test.c -o test [muxue@bt-7274:~/GIT/raspi/vim/TestMake]$ ls makefile test test.c [muxue@bt-7274:~/GIT/raspi/vim/TestMake]$ ./test hello wolrd! Add(a,b) ret: 21
我们还可以写一个清除指令,用于在编译后删除大量临时出现的可执行程序
1 2 3
.PHONY:clean clean: rm -f test
在原本的makefile后追加这部分内容即可
通过make clean来清理文件
1 2 3 4 5 6
[muxue@bt-7274:~/GIT/raspi/vim/TestMake]$ ls makefile test test.c [muxue@bt-7274:~/GIT/raspi/vim/TestMake]$ make clean rm -f test [muxue@bt-7274:~/GIT/raspi/vim/TestMake]$ ls makefile test.c
4.1出现missing separator解决方案
当我执行make clean的时候出现了这个报错
1 2
[muxue@bt-7274:~/GIT/raspi/vim/TestMake]$ make clean makefile:4: *** missing separator. Stop.
[muxue@bt-7274:~/GIT/raspi/vim/TestMake]$ ls makefile test.c [muxue@bt-7274:~/GIT/raspi/vim/TestMake]$ make gcc test.c -o test [muxue@bt-7274:~/GIT/raspi/vim/TestMake]$ ls makefile test test.c [muxue@bt-7274:~/GIT/raspi/vim/TestMake]$ make make: `test' is up to date. [muxue@bt-7274:~/GIT/raspi/vim/TestMake]$