一、样例代码
// mycmd.c
#include <stdio.h>
int Sum(int s, int e) {
int result = 0;
for (int i = s; i <= e; i++) {
result += i;
}
return result;
}
int main() {
int start = 1;
int end = 100;
printf("I will begin\n");
int n = Sum(start, end);
printf("running done, result is: [%d-%d]=%d\n", start, end, n);
return 0;
}
二、预备
程序的发布方式有两种,debug 模式和 release 模式。Linux gcc/g++ 出来的二进制程序默认是 release 模式。
要使用 gdb 调试,必须在源代码生成二进制程序的时候加上 -g 选项,如果没有添加,程序无法被调试。
$ gcc mycmd.c -o mycmd # 默认模式,不支持调试
$ file mycmd
mycmd: ELF 64-bit LSB shared object, x86-64, version 1(SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=82f5cbaada10a9987d9f325384861a88d278b160, for GNU/Linux 3.2.0, not stripped
$ gcc mycmd.c -o mycmd -g # debug 模式
$ file mycmd
mycmd: ELF 64-bit LSB shared object, x86-64, version 1(SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=3d5a2317809ef86c7827e9199cfefa622e3c187, for GNU/Linux 3.2.0, with debug_info, not stripped
三、常见使用
- 开始:
gdb binFile - : 或 调试命令


