GCC/GDB编译及调试

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/BluseLIBB/article/details/98075021

安装gdb

gcc编译器
vim test.c

#include<stdio.h>
void main()
{
        char msg[80]="Hello , World !!";
        printf("%s\n", msg);
}

gcc test.c 编译生成a.out可执行文件 ./a.out运行程序
gcc -E test.c -o test.i 预处理 宏替换
gcc -S test.i -o test.s 编译 语法检查 生成汇编文件
gcc -c test.s -o test.o 汇编 将汇编文件转换成目标文件(机器码)
gcc -O test.0 -o test 链接 链接库和其他目标文件

gcc编译出错的几种可能
第一类:语法错误
第二类:头文件错误
第三类:档案库错误 归档错误 当我们引用某个库时,报错 math 线程库 用-l指定库
第四类:为定义符号

gcc组件:
1、编译器
2、汇编器
3、连接器
4、C标准库

链接库文件
-lpthread -lm
指定头文件目录 -I
指定库文件目录 -L
优化选项
-O1 -O2 -O3 优化等级
time +程序 查看运行时间

gdb调试器
vim test.c

#include<stdio.h>
int sum(int n);
main()
{
        int s=0;
        int i,n;
        for(i=0;i<=50;i++)
        {
                s=i+s;
        }
        s=s+sum(20);
        printf("the result is %d \n", s);
}
int sum(int n)
{
        int total=0;
        int i;
        for(i=0; i<=n; i++)
        {
                total=total+i;
        }
        return total;
}

gcc -g test.c -o test 编译时加入调试选项
gdb test 开始gdb调试
l 查看源代码
break 7 b 7在第7行设置断点
break sum 在sum处设置断点
info break info b显示断点信息 info b
del 1 去掉第一个断点
run r运行程序
next n下一行
print s p输出s的值
continue c继续执行到下一个断点
backtrace 栈回溯查找错误
q 退出gdb调试

猜你喜欢

转载自blog.csdn.net/BluseLIBB/article/details/98075021