Welcome 微信登录

首页 / 操作系统 / Linux / Linux下GCC的DEBUG和优化,以及编译过程

第一:DEBUG实例[root@localhost debug]# vim null.c int a(int *p); int main(void){        int* p = 0;        return a(p);}int a(int *p){        int y = *p;        return y;}~  [root@localhost debug]# ulimit -c unlimited[root@localhost debug]# gcc -Wall  –g null.c [root@localhost debug]# ./a.out Segmentation fault (core dumped)[root@localhost debug]# lsnull.c  a.out  core.21982  [root@localhost debug]# yum -y install gdb[root@localhost debug]# gdb a.out core.21982……………..……………..……………..(gdb) print pNo symbol table is loaded.  Use the "file" command.(gdb) print p$1 = (int *) 0x0(gdb) backtrace#0  0x08048389 in a (p=0x0) at null.c:10#1  0x08048377 in main () at null.c:6(gdb) quit (gdb)第二:优化实例(这里主要采用的是-O0—3和no-loops方式)[root@localhost opm]# cat test.c #include <stdio.h> double powern(double d, unsigned n){       double x = 1.0;       unsigned j;        for(j=1;j<n;j++)              x *=d;       return x;} int main(void){       double sum = 0.0;       unsigned i;        for(i=1;i<=100000000;i++ )       {              sum += powern(i,i%5);       }       printf("sum = %g ",sum);       return 0;}[root@localhost opm]# gcc -Wall -O0 test.c -o o0  //优化等级为0---不优化[root@localhost opm]# time ./o0 sum = 5e+30 real  0m2.815suser 0m2.799ssys   0m0.013s[root@localhost opm]# gcc -Wall -O1 test.c -o o1  //优化等级为1[root@localhost opm]# time ./o1 sum = 5e+30 real  0m1.849suser 0m1.843ssys   0m0.006s[root@localhost opm]# gcc -Wall -O2 test.c -o o2  //优化等级为2[root@localhost opm]# time ./o2 sum = 5e+30 real  0m1.923suser 0m1.910ssys   0m0.011s[root@localhost opm]# gcc -Wall -O3 test.c -o o3  //优化等级为1[root@localhost opm]# time ./o3sum = 5e+30 real  0m1.460suser 0m1.453ssys   0m0.006s[root@localhost opm]# gcc -Wall -O3 -funroll-loops  test.c -o o4  //加入no—loop优化[root@localhost opm]# time ./o4sum = 5e+30 real  0m1.322suser 0m1.308ssys   0m0.014s第三:优化帮助发现DEBUG[root@localhost O]# vim uninit.c  int sign(int x){        int s;        if (x>0)                s = 1;        else if (x<0)                s = -1;        return s;}[root@localhost O]# gcc -Wall -c uninit.c [root@localhost O]# gcc -Wall -O1 -c uninit.c uninit.c: In function "sign":uninit.c:3: warning: "s" may be used uninitialized in this function[root@localhost O]# gcc -Wall -O2 -c uninit.c uninit.c: In function "sign":uninit.c:3: warning: "s" may be used uninitialized in this function[root@localhost O]# gcc -Wall -O3 -c uninit.c uninit.c: In function "sign":uninit.c:3: warning: "s" may be used uninitialized in this function编译过程描述实例[root@localhost hello]# vim hello.c  #include <stdio.h> int main(void){        printf("Hello World!!! ");        return 0;}第四:最后列举下编译的过程实例1、  预处理器阶段(cpp hello.c > hello.i)[root@localhost hello]# cpp hello.c > hello.i2、  编译器阶段(gcc -Wall -S hello.i)[root@localhost hello]# gcc -Wall -S hello.i3、  汇编器阶段(as hello.s -o hello.o)[root@localhost hello]# as hello.s -o hello.o4、  连接器阶段(gcc hello.o)[root@localhost hello]# gcc hello.o[root@localhost hello]# ./a.out Hello World!!![root@localhost hello]# file a.out   //检测可执行文件的信息a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.6.9, dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped[root@localhost hello]# ldd a.out //检查用到的库文件       linux-gate.so.1 =>  (0x002ab000)       libc.so.6 => /lib/libc.so.6 (0x00b3a000)       /lib/ld-linux.so.2 (0x00b17000)结束Linux下GCC程序优化工具gprof和gcov实例Linux 下GCC的于处理器CPP使用实例相关资讯      gcc 
  • gcc: error trying to exec "  (今 06:25)
  • GCC 6.1带来新的C++17特性、完全支  (05月06日)
  • Linux环境下使用GCC编译,GDB反汇  (03月30日)
  • 用GCC进行程序的编译  (06月14日)
  • GNU编译器套件GCC 6.1发布 默认使  (04月28日)
  • 让GCC支持成员函数模板的trick  (03月10日)
本文评论 查看全部评论 (0)
表情: 姓名: 字数

<