编译过程简介

来源:互联网 发布:淘宝网新闻今日头条 编辑:程序博客网 时间:2024/05/22 11:43

编译预处理

专题三:编译预处理。包括以下章节:

  • 编译过程简介
  • 宏定义与宏使用分析
  • 条件编译使用分析
  • #error和#line
  • #pragma预处理分析
  • #和##运算符使用解析

你不知道的事

  • gcc file.c file.h命令的过程
    这里写图片描述

预编译

  • 处理所有的注释,用空格替换
  • 处理#define宏定义:删除所有宏定义,展开所有宏定义
  • 处理条件编译指令#if,#ifdef,#elif,#else,#elseif
  • 处理#include:展开被包含的文件
  • 保留预编译器需要使用的#pragma

    预处理指令:gcc -E file.c -o file.i

编译

  • 对预处理文件进行一系列词法分析,语法分析和语义分析
    • 词法分析主要分析关键字,标示符,立即数等是否合法
    • 语法分析主要分析表达式是否遵循语法规则
    • 语义分析在语法分析基础上进一步分析表达式是否合法
  • 分析结束后进行代码优化生成相应的汇编代码文件

    预处理指令:gcc -S file.c -o file.s

汇编

  • 汇编器将汇编代码转变为机器码(机器可以执行的指令)

    • 每个汇编语句几乎对应一条机器指令

    预处理指令:gcc -c file.s -o file.o

链接器

  • 链接器主要是将有关各个模块的目标文件彼此相连接生成可加载、可执行的目标文件。
  • 链接器有两种链接方式:静态链接和动态链接
    • 静态链接:链接器将函数的代码从其所在地(目标文件或静态链接库中)拷贝到最终的可执行程序中。
    • 动态链接:只提供符号表和其他少量信息用于保证所有符号引用都有定义,保证编译顺利通过。在运行时此可执行文件被执行时,动态链接库的全部内容将被映射到运行时相应进程的虚地址空间。
  • 静态链接和动态链接优缺点
    • **静态链接:优点:在编译阶段已经完成拷贝,不需要重定位目标文件,节省时间;
    • 缺点:目标文件会很大,占用更多内存**
    • **动态链接:优点:更新动态库,无需重新链接;运行中可供多个程序使用,内存中只需要有一份,节省内存。
    • 缺点:对于大系统,重新链接是一个非常耗时的过程**

小结

  • 编译器的编译工作主要分为预处理,编译,汇编三部分
  • 链接器的工作是把各个独立的模块链接为可执行文件
  • 静态链接在编译器完成,动态链接在运行期完成

实例分析1-1:源代码单步编译示例

hello.c

#include <stdio.h>#define HELLOWORLD "hello world!\n"int main(){    printf(HELLOWORLD);    return 0;}

执行命令:gcc -E hello.c -o hello.i

hello.i

# 1 "hello.c"# 1 "<built-in>"# 1 "<命令行>"# 1 "hello.c"# 1 "/usr/include/stdio.h" 1 3 4# 28 "/usr/include/stdio.h" 3 4# 1 "/usr/include/features.h" 1 3 4# 324 "/usr/include/features.h" 3 4# 1 "/usr/include/x86_64-linux-gnu/bits/predefs.h" 1 3 4# 325 "/usr/include/features.h" 2 3 4# 357 "/usr/include/features.h" 3 4# 1 "/usr/include/x86_64-linux-gnu/sys/cdefs.h" 1 3 4# 378 "/usr/include/x86_64-linux-gnu/sys/cdefs.h" 3 4# 1 "/usr/include/x86_64-linux-gnu/bits/wordsize.h" 1 3 4# 379 "/usr/include/x86_64-linux-gnu/sys/cdefs.h" 2 3 4# 358 "/usr/include/features.h" 2 3 4# 389 "/usr/include/features.h" 3 4# 1 "/usr/include/x86_64-linux-gnu/gnu/stubs.h" 1 3 4# 1 "/usr/include/x86_64-linux-gnu/bits/wordsize.h" 1 3 4# 5 "/usr/include/x86_64-linux-gnu/gnu/stubs.h" 2 3 4# 1 "/usr/include/x86_64-linux-gnu/gnu/stubs-64.h" 1 3 4# 10 "/usr/include/x86_64-linux-gnu/gnu/stubs.h" 2 3 4# 390 "/usr/include/features.h" 2 3 4# 29 "/usr/include/stdio.h" 2 3 4# 1 "/usr/lib/gcc/x86_64-linux-gnu/4.6/include/stddef.h" 1 3 4# 212 "/usr/lib/gcc/x86_64-linux-gnu/4.6/include/stddef.h" 3 4typedef long unsigned int size_t;# 35 "/usr/include/stdio.h" 2 3 4# 1 "/usr/include/x86_64-linux-gnu/bits/types.h" 1 3 4# 28 "/usr/include/x86_64-linux-gnu/bits/types.h" 3 4# 1 "/usr/include/x86_64-linux-gnu/bits/wordsize.h" 1 3 4# 29 "/usr/include/x86_64-linux-gnu/bits/types.h" 2 3 4typedef unsigned char __u_char;typedef unsigned short int __u_short;typedef unsigned int __u_int;typedef unsigned long int __u_long;typedef signed char __int8_t;typedef unsigned char __uint8_t;typedef signed short int __int16_t;typedef unsigned short int __uint16_t;typedef signed int __int32_t;typedef unsigned int __uint32_t;typedef signed long int __int64_t;typedef unsigned long int __uint64_t;typedef long int __quad_t;typedef unsigned long int __u_quad_t;# 131 "/usr/include/x86_64-linux-gnu/bits/types.h" 3 4# 1 "/usr/include/x86_64-linux-gnu/bits/typesizes.h" 1 3 4# 132 "/usr/include/x86_64-linux-gnu/bits/types.h" 2 3 4typedef unsigned long int __dev_t;typedef unsigned int __uid_t;typedef unsigned int __gid_t;typedef unsigned long int __ino_t;typedef unsigned long int __ino64_t;typedef unsigned int __mode_t;typedef unsigned long int __nlink_t;typedef long int __off_t;typedef long int __off64_t;typedef int __pid_t;typedef struct { int __val[2]; } __fsid_t;typedef long int __clock_t;typedef unsigned long int __rlim_t;typedef unsigned long int __rlim64_t;typedef unsigned int __id_t;typedef long int __time_t;typedef unsigned int __useconds_t;typedef long int __suseconds_t;typedef int __daddr_t;typedef long int __swblk_t;typedef int __key_t;typedef int __clockid_t;typedef void * __timer_t;typedef long int __blksize_t;typedef long int __blkcnt_t;typedef long int __blkcnt64_t;typedef unsigned long int __fsblkcnt_t;typedef unsigned long int __fsblkcnt64_t;typedef unsigned long int __fsfilcnt_t;typedef unsigned long int __fsfilcnt64_t;typedef long int __ssize_t;typedef __off64_t __loff_t;typedef __quad_t *__qaddr_t;typedef char *__caddr_t;typedef long int __intptr_t;typedef unsigned int __socklen_t;# 37 "/usr/include/stdio.h" 2 3 4# 45 "/usr/include/stdio.h" 3 4struct _IO_FILE;typedef struct _IO_FILE FILE;# 65 "/usr/include/stdio.h" 3 4typedef struct _IO_FILE __FILE;# 75 "/usr/include/stdio.h" 3 4# 1 "/usr/include/libio.h" 1 3 4# 32 "/usr/include/libio.h" 3 4# 1 "/usr/include/_G_config.h" 1 3 4# 15 "/usr/include/_G_config.h" 3 4# 1 "/usr/lib/gcc/x86_64-linux-gnu/4.6/include/stddef.h" 1 3 4# 16 "/usr/include/_G_config.h" 2 3 4# 1 "/usr/include/wchar.h" 1 3 4# 83 "/usr/include/wchar.h" 3 4typedef struct{  int __count;  union  {    unsigned int __wch;    char __wchb[4];  } __value;} __mbstate_t;# 21 "/usr/include/_G_config.h" 2 3 4typedef struct{  __off_t __pos;  __mbstate_t __state;} _G_fpos_t;typedef struct{  __off64_t __pos;  __mbstate_t __state;} _G_fpos64_t;# 53 "/usr/include/_G_config.h" 3 4typedef int _G_int16_t __attribute__ ((__mode__ (__HI__)));typedef int _G_int32_t __attribute__ ((__mode__ (__SI__)));typedef unsigned int _G_uint16_t __attribute__ ((__mode__ (__HI__)));typedef unsigned int _G_uint32_t __attribute__ ((__mode__ (__SI__)));# 33 "/usr/include/libio.h" 2 3 4# 53 "/usr/include/libio.h" 3 4# 1 "/usr/lib/gcc/x86_64-linux-gnu/4.6/include/stdarg.h" 1 3 4# 40 "/usr/lib/gcc/x86_64-linux-gnu/4.6/include/stdarg.h" 3 4typedef __builtin_va_list __gnuc_va_list;# 54 "/usr/include/libio.h" 2 3 4# 172 "/usr/include/libio.h" 3 4struct _IO_jump_t; struct _IO_FILE;# 182 "/usr/include/libio.h" 3 4typedef void _IO_lock_t;/*......此处省略n行*/extern void funlockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__));# 940 "/usr/include/stdio.h" 3 4 ///此处以上都是<stdio.h>展开的内容# 2 "hello.c" 2//此处删除了宏定义#define HELLOWORLDint main(){    printf("hello world!\n");//宏定义#define HELLOWORLD直接替换    return 0;}

为了更容易理解,修改hello.c

hello.c

//#include <stdio.h>#define HELLOWORLD "hello world!\n"int main(){    printf(HELLOWORLD);    return 0;}

预编译处理:执行命令:gcc -E hello.c -o hello.i
hello.i

//此处删除了注释//#include <stdio.h>//以下是一些说明文字,不会执行# 1 "hello.c"# 1 "<built-in>"# 1 "<命令行>"# 1 "hello.c"int main(){    printf("hello world!\n");//预处理器直接替换宏定义    return 0;}

编译处理:执行命令:gcc -S hello.c -o hello.s 或者 gcc -S hello.i -o hello.s
hello.s

    .file   "hello.c"    .section    .rodata.LC0:    .string "hello world!"    .text    .globl  main    .type   main, @functionmain:.LFB0:    .cfi_startproc    pushq   %rbp    .cfi_def_cfa_offset 16    .cfi_offset 6, -16    movq    %rsp, %rbp    .cfi_def_cfa_register 6    movl    $.LC0, %edi    call    puts    movl    $0, %eax    popq    %rbp    .cfi_def_cfa 7, 8    ret    .cfi_endproc.LFE0:    .size   main, .-main    .ident  "GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3"    .section    .note.GNU-stack,"",@progbits

汇编处理:执行命令:gcc -c hello.s -o hello.o 或者 gcc -c hello.c -o hello.o
hello.o

//二进制代码,无法打开(都是1和0)。
原创粉丝点击