当前位置: > Linux服务器 > Linux系统 >

Linux下GCC的DEBUG和优化,以及编译过程

时间:2014-10-30 12:04来源:linux.it.net.cn 作者:it

第一: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]# ls

null.c  a.out  core.21982  

[root@localhost debug]# yum -y install gdb

[root@localhost debug]# gdb a.out core.21982

……………..

……………..

……………..

(gdb) print p

No 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\n",sum);

       return 0;

}

[root@localhost opm]# gcc -Wall -O0 test.c -o o0  //优化等级为0---不优化

[root@localhost opm]# time ./o0 

sum = 5e+30

 

real  0m2.815s

user 0m2.799s

sys   0m0.013s

[root@localhost opm]# gcc -Wall -O1 test.c -o o1  //优化等级为1

[root@localhost opm]# time ./o1 

sum = 5e+30

 

real  0m1.849s

user 0m1.843s

sys   0m0.006s

[root@localhost opm]# gcc -Wall -O2 test.c -o o2  //优化等级为2

[root@localhost opm]# time ./o2 

sum = 5e+30

 

real  0m1.923s

user 0m1.910s

sys   0m0.011s

[root@localhost opm]# gcc -Wall -O3 test.c -o o3  //优化等级为1

[root@localhost opm]# time ./o3

sum = 5e+30

 

real  0m1.460s

user 0m1.453s

sys   0m0.006s

[root@localhost opm]# gcc -Wall -O3 -funroll-loops  test.c -o o4  //加入no—loop优化

[root@localhost opm]# time ./o4

sum = 5e+30

 

real  0m1.322s

user 0m1.308s

sys   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!!!\n");

        return 0;

}

第四:最后列举下编译的过程实例

1、  预处理器阶段(cpp hello.c > hello.i)

[root@localhost hello]# cpp hello.c > hello.i

2、  编译器阶段(gcc -Wall -S hello.i)

[root@localhost hello]# gcc -Wall -S hello.i

3、  汇编器阶段(as hello.s -o hello.o)

[root@localhost hello]# as hello.s -o hello.o

4、  连接器阶段(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)

结束

(责任编辑:IT)
------分隔线----------------------------