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