歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux下GCC的DEBUG和優化,以及編譯過程

Linux下GCC的DEBUG和優化,以及編譯過程

日期:2017/2/28 16:00:34   编辑:Linux教程

第一: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)

結束

Copyright © Linux教程網 All Rights Reserved