gcc學習與實踐 2

2021-06-05 18:41:09 字數 4449 閱讀 1756

主要學習如下gcc選項:

-c、-m、-mm、-md、-mmd、編譯優化選項(-o0、-o1、-o2、-o3)、-wall

8、-c 選項

在預處理的時候,不刪除注釋資訊,一般和-e一起使用。有時候分析程式,比較方便。

比如,加上注釋的這段程式helloworld.c程式如下:

#include

int main()

如果只是使用-e選項進行編譯:

[root@shirdrn junys]# gcc -e helloworld.c > helloworld.c.ignorecomments.txt

則會忽略注釋,從而加快預處理過程,匯出生成的helloworld.c.ignorecomments.txt檔案就比使用-c和-e一起進行編譯生成的匯出檔案大很多:

[root@shirdrn junys]# gcc -c -e helloworld.c > helloworld.c.txt

helloworld.c.txt檔案為2582行,helloworld.c.ignorecomments.txt檔案為934行。

9、 -m 選項

生成當前編譯程式檔案關聯的詳細資訊,包含目標檔案所依賴的所有源**檔案,包括標頭檔案。

比如,使用-m選項進行編譯:

[root@shirdrn junys]# gcc -m helloworld.c

生成的關聯檔案的資訊如下所示:

helloworld.o: helloworld.c /usr/include/stdio.h /usr/include/features.h \

/usr/include/sys/cdefs.h /usr/include/bits/wordsize.h \

/usr/include/gnu/stubs.h /usr/include/gnu/stubs-32.h \

/usr/lib/gcc/i386-redhat-linux/4.1.2/include/stddef.h \

/usr/include/bits/types.h /usr/include/bits/typesizes.h \

/usr/include/libio.h /usr/include/_g_config.h /usr/include/wchar.h \

/usr/include/bits/wchar.h /usr/include/gconv.h \

/usr/lib/gcc/i386-redhat-linux/4.1.2/include/stdarg.h \

/usr/include/bits/stdio_lim.h /usr/include/bits/sys_errlist.h

10、-mm 選項

同-m選項一樣,不同的是,它將忽略由#include 包含的標頭檔案所造成的檔案之間的依賴關係。

比如,使用-mm選項進行編譯:

[root@shirdrn junys]# gcc -mm helloworld.c

只要與#include 中有關聯的檔案資訊都不會顯示出來,如下所示:

[root@shirdrn junys]# gcc -mm helloworld.c

helloworld.o: helloworld.c

[root@shirdrn junys]#

11、-md 選項

同-m選項相同,生成當前編譯程式檔案關聯的詳細資訊,包含目標檔案所依賴的所有源**檔案,包括標頭檔案,但是,資訊輸出將匯入到.d的檔案裡面。

比如使用-md選項進行編譯:

[root@shirdrn junys]# gcc -md helloworld.c

編譯結果是,生成乙個helloworld.d檔案,該檔案包含了於程式檔案關聯的檔案的資訊,helloworld.d檔案內容如下所示:

helloworld.o: helloworld.c /usr/include/stdio.h /usr/include/features.h \

/usr/include/sys/cdefs.h /usr/include/bits/wordsize.h \

/usr/include/gnu/stubs.h /usr/include/gnu/stubs-32.h \

/usr/lib/gcc/i386-redhat-linux/4.1.2/include/stddef.h \

/usr/include/bits/types.h /usr/include/bits/typesizes.h \

/usr/include/libio.h /usr/include/_g_config.h /usr/include/wchar.h \

/usr/include/bits/wchar.h /usr/include/gconv.h \

/usr/lib/gcc/i386-redhat-linux/4.1.2/include/stdarg.h \

/usr/include/bits/stdio_lim.h /usr/include/bits/sys_errlist.h

12、-mmd 選項

同-mm選項相同,將忽略由#include 包含的標頭檔案所造成的檔案之間的依賴關係,但是,資訊輸出將匯入到.d的檔案裡面。

比如使用-mmd選項進行編譯:

[root@shirdrn junys]# gcc -md helloworld.c

編譯結果是,生成乙個helloworld.d檔案,該檔案包含了於程式檔案關聯的檔案的資訊,helloworld.d檔案內容如下所示:   

helloworld.o: helloworld.c

13、優化選項

編譯器優化主要有下面四個選項:

-o0

-o1

-o2

-o3-o0選項表示沒有優化;-o1選項為預設值;-o3選項表示優化級別最高。

但是,需要明確一點,優化級別越高,編譯的速度會越慢的。

使用比這段程式helloworld.c程式如下:

#include

int main()

通過使用o0和o3級別的優化選項進行一下對比吧。

使用o0優化級別進行編譯,如下所示:

[root@shirdrn junys]# time gcc -o o0helloworld -o0 helloworld.c

real    0m1.003s

user    0m0.142s

sys     0m0.061s

使用o3優化級別進行編譯,如下所示:

[root@shirdrn junys]# time gcc -o o3helloworld -o3 helloworld.c

real    0m1.067s

user    0m0.142s

sys     0m0.053s

優化級別越高,執行程式的效率是越高的,可以通過使用上面的兩個級別編譯的程式,進行一下執行效率的對比,如下所示:

[root@shirdrn junys]# time ./o0helloworld

hello,the world!

real    0m0.014s

user    0m0.000s

sys     0m0.002s

[root@shirdrn junys]# time ./o3helloworld

hello,the world!

real    0m0.003s

user    0m0.000s

sys     0m0.003s

由於helloworld.c這個程式過於簡單了,所以測試比較不是很明顯,你可以嘗試使用乙個更加複雜的程式進行測試一下。

14、-wall 選項

用於輸出警告資訊。

例如,這段程式helloworld.c,沒有返回語句return 0;,程式如下:

#include

int main()

正常情況下進行編譯,沒有任何錯誤資訊,編譯通過,但是如果指定了-wall選項進行編譯,會輸出警告資訊:

[root@shirdrn junys]# gcc -o helloworld -wall helloworld.c

helloworld.c: 在函式 『main』 中:

helloworld.c:8: 警告:在有返回值的函式中,控制流程到達函式尾

再比如,在程式中定義了變數但是沒有使用到:

#include

int main()

也會提示警告資訊:

[root@shirdrn junys]# gcc -o helloworld -wall helloworld.c

helloworld.c: 在函式 『main』 中:

helloworld.c:6: 警告:未使用的變數 『cnt』

helloworld.c:9: 警告:在有返回值的函式中,控制流程到達函式尾

gcc使用與分析2

gcc使用與分析1 gcc v o hello hello.o 用來顯示編譯過程 顯示編譯過程會發現許多crt1.o crti.o crtbegin.o crtend.o crtn.o這樣的obj檔案,這些檔案是gcc為了讓可執行檔案可以在linux平台執行加的系統標準啟動檔案,對於一般應用程式,這...

GCC的認識與學習

gcc的認識與學習 階段 輸入 輸出 gcc例項 預編譯 c i gcc e test.c otest.i 編譯 i s gcc s test.i otest.s 彙編 s o gcc c test.s otest.o 鏈結 o gcc test.o o test 預處理階段 原始檔 c 與相應的包...

Struts原理與實踐(2)

page contenttype text html charset utf 8 taglib uri web inf struts bean.tld prefix bean taglib uri web inf struts html.tld prefix html html head title...