GCC編譯器

詳見《gcc中文手冊》性能

編譯過程優化

    預處理器cpp    編譯器gcc    彙編器as     連接器linker   設計

file.c   -------------> file.i  ------------->file.s ----------->file.o -------------->file.out調試

file.h                          libc.ablog

 

gcc選項概述
man gcc                 查看更多選項信息
gcc [options] [filename]
    -x language
    -c                    只對文件進行編譯和彙編,不連接
    -S                    只對文件進行編譯,不彙編和連接
    -E                    只對文件進行預處理
    -o [file] file2     
    -lname  (小寫L)用來指定所使用的庫文件  例:-lm 使用libm.a (m就是庫的名稱)ip

 -Idirname         將dirname所指出的目錄加入到程序頭文件目錄列表中 編譯器

                      例:gcc foo.c -I /home/include -o foo                     
    -Ldirname       將dirname所指出的目錄加到庫文件的目錄列表中。pip

                      例:gcc foo.c -L /home/lib -lfoo -o fooio

  -static      靜態連接(將庫的內容加入程序中,造成完整的可執行程序)
    -w                    禁止生成警告信息
    -Wall                顯示附加的警告信息for循環

  -Dmacro    定義MACRO宏,等效於在程序中使用#define MACRO  

                      例:gcc -DDEBUG hello.c -o hello  

                        gcc -DNUM=2 hello.c -o hello

  -pedantic         嚴格要求符合ANSI標準
    -g                     添加調試信息
    -p                     產生prof所需的信息
    -pg                    產生gpof所使用的信息
    -O(-O1)             對編譯出的代碼進行優化
    -O2                 進行比-O高一級的優化
    -O3                 產生更高級別的優化
    -v                  
    -m***                根據不一樣的微處理器進行優化

詳解:
    gcc -c test.c                生成.o文件
    gcc -S test.c                生成彙編文件.s
    gcc -E test.c -o test.i     生成.i
    gcc -V 2.6.3 -v              強制執行2.6.3版本
    gcc -m486                     使用對486的相應優化效果

    gcc -Wall -o test test.c

    gcc -g -Wall -o test3_1 test3_1.c
    gcc -ggdb3 -Wall -o test3_1 test3_1.c     -ggdb3使用最高級別的調試信息

    高級gcc選項
    1.管理大型項目(多模塊的編譯)
        gcc -Wall -c -o test3_1 test3_1.c
        gcc -Wall -c -o test3_2 test3_2.c
        gcc -Wall -c -o test3_3 test3_3.C
        gcc -o test test3_1.o test3_2.o test3_3.o
    2.指定查找路徑 (-I -L)
        gcc -Wall -I/usr/include/zw -o test test.c
        gcc -Wall -L/usr/X11R6/lib -Wall -o test test.c -IX11
    3.連接庫(-l) l連接的庫能夠是靜態的也能夠是共享的。

  gcc -o test test3a.o test3b.o test3.o -lm
        
    4.使用管道(使管道前的輸出成爲管道後的輸入,能夠同時調用多個程序) ?
        gcc -pipe -Wall -O3 -o test test.c  
        
Gcc編譯流程
    C預處理    (C預處理器cpp)
    Gcc     (gcc)
    彙編     (as)
    文件處理創建靜態庫    (ar)
    GUN連接    (ld)
    
    輔助:
    庫顯示    (ldd)

/*************************
此程序設計的性能很低。用於比較優化先後的性能

致使程序低效的緣由:
for循環的結束值及步長每次都要從新計算
five變量沒有必要每次循環都爲它分配值,只要在循環前作一次賦值便可

**************************/

#include <stdio.h>

int main(void)
{
	int counter;
	int ending;
	int temp;
	int five;
	for(counter=0;counter<2*100000000*9/18+5131;counter+=(5-3)/2)
	{
		temp=counter/15302;
		ending=counter;
		five=5;
	}
	printf("five=%d;ending=%d\n;temp=%d",five,ending,temp);
	return 0;
}

 帶優化與不帶優化的編譯差異

//test3_2.c 程序優化
gcc -Wall -o test3_2 test3_2.c
time ./test3_2 					//查看程序運行時間

gcc -Wall -O1 -o test3_2pro test3_2.c
time ./test3_2pro
相關文章
相關標籤/搜索