zoukankan      html  css  js  c++  java
  • GCC编译器

    详见《gcc中文手册》

    编译过程

        预处理器cpp    编译器gcc    汇编器as     链接器linker   

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

    file.h                          libc.a

    gcc选项概述
    man gcc                 查看更多选项信息
    gcc [options] [filename]
        -x language
        -c                    只对文件进行编译和汇编,不链接
        -S                    只对文件进行编译,不汇编和链接
        -E                    只对文件进行预处理
        -o [file] file2     
        -lname  (小写L)用来指定所使用的库文件  例:-lm 使用libm.a (m就是库的名称)

     -Idirname         将dirname所指出的目录加入到程序头文件目录列表中

                          例:gcc foo.c -I /home/include -o foo                     
        -Ldirname       将dirname所指出的目录加到库文件的目录列表中。

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

      -static      静态链接(将库的内容加入程序中,形成完整的可执行程序)
        -w                    禁止生成警告信息
        -Wall                显示附加的警告信息

      -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
    ;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
    
  • 相关阅读:
    git或gitee 提交代码到远程仓库
    gitee 创建代码仓库,并提交本地代码
    Logback 实现日志链路追踪
    navicat 查看,设计并导出数据库 ER图
    Jackson 使用 @JsonFormat 注解进行时间格式化
    Redis 缓存常见问题
    jedis 与 redission 实现分布式锁
    Redis 集群模式搭建
    Redis 哨兵模式高可用
    Notepad++ 正则表达式提取信息
  • 原文地址:https://www.cnblogs.com/kwseeker-bolgs/p/4366684.html
Copyright © 2011-2022 走看看