系统

C语言编译过程

2017-08-03  本文已影响4人  第八区

示例代码

#include <stdio.h>
#include "head.h"
/*
annotation one
annotation two
*/
extern int N;
int main(){
    printf("build test N=%d\n",N);
    printStr("abc");
    getchar();
}
#ifndef HEAD_H
#define HEAD_H
int N=100;
void printStr(char *);
#endif
#include<stdio.h>
void printStr(char *str){
    printf("%s\n",str);
}

预处理

处理关于 “#” 的指令

...省略部分代码
# 2 "a.c" 2
# 1 "head.h" 1



# 3 "head.h"
int N=100;
void printStr(char *);
# 3 "a.c" 2




extern int N;
int main(){
 printf("build test N=%d\n",N);
 printStr("abc");
 getchar();
}

预编译结果解释
# linenum filename flags
分别对应行号、文件、标识。
flag对应的含义

  1. 文件的开始
  2. 文件的返回(在include另一个文件后)
  3. 代表接下来的文本来自系统头文件,所以某些警告应该禁止。
  4. 代表接下来的文本应该被当做包含一个隐式的extern "C

编译

1.高级语言->汇编代码
    .file   "a.c"
    .globl  _N
    .data
    .align 4
_N:
    .long   100
    .def    ___main;    .scl    2;  .type   32; .endef
    .section .rdata,"dr"
LC0:
    .ascii "build test N=%d\12\0"
LC1:
    .ascii "abc\0"
    .text
    .globl  _main
    .def    _main;  .scl    2;  .type   32; .endef
_main:
LFB10:
    .cfi_startproc
    pushl   %ebp
    .cfi_def_cfa_offset 8
    .cfi_offset 5, -8
    movl    %esp, %ebp
    .cfi_def_cfa_register 5
    andl    $-16, %esp
    subl    $16, %esp
    call    ___main
    movl    _N, %eax
    movl    %eax, 4(%esp)
    movl    $LC0, (%esp)
    call    _printf
    movl    $LC1, (%esp)
    call    _printStr
    call    _getchar
    movl    $0, %eax
    leave
    .cfi_restore 5
    .cfi_def_cfa 4, 4
    ret
    .cfi_endproc
LFE10:
    .ident  "GCC: (GNU) 5.3.0"
    .def    _printf;    .scl    2;  .type   32; .endef
    .def    _printStr;  .scl    2;  .type   32; .endef
    .def    _getchar;   .scl    2;  .type   32; .endef
2.汇编代码->机器代码

链接

使用到了C标准库的东西“printf”,但是编译过程只是把源文件翻译成二进制而已,这个二进制还不能直接执行,这个时候就需要做一个动作,将翻译成的二进制与需要用到库绑定在一块。
函数库一般分为静态库和动态库两种

  1. 静态库是指编译链接时,把库文件的代码全部加入到可执行文件中,因此生成的文件比较大,但在运行时也就不再需要库文件了。其后缀名一般为”.a”。
  2. 动态库与之相反,在编译链接时并没有把库文件的代码加入到可执行文件中,而是在程序执行时由运行时链接文件加载库,这样可以节省系统的开销。动态库一般后缀名为”.so”,如前面所述的libc.so.6就是动态库。Gcc在编译时默认使用动态库。
静态库链接时搜索路径顺序:
  1. ld会去找GCC命令中的参数-L
  2. 再找gcc的环境变量LIBRARY_PATH
  3. 再找内定目录 /lib /usr/lib /usr/local/lib 这是当初compile gcc时写在程序内的
动态链接时、执行时搜索路径顺序:
  1. 编译目标代码时指定的动态库搜索路径
  2. 环境变量LD_LIBRARY_PATH指定的动态库搜索路径
  3. 配置文件/etc/ld.so.conf中指定的动态库搜索路径
  4. 默认的动态库搜索路径/lib
  5. 默认的动态库搜索路径/usr/lib
有关环境变量:

LIBRARY_PATH环境变量:指定程序静态链接库文件搜索路径
LD_LIBRARY_PATH环境变量:指定程序动态链接库文件搜索路径

说下生成静态库的方法:  
    ar cr libxxx.a file1.o file2.o  
就是把file1.o和file2.o打包生成libxxx.a静态库  
使用的时候  
    gcc test.c -L/path -lxxx -o test  
  
动态库的话:  
    gcc -fPIC -shared file1.c -o libxxx.so  
也可以分成两部来写:  
    gcc -fPIC file1.c -c //这一步生成file1.o  
    gcc -shared file1.o -o libtest.so  
上一篇 下一篇

猜你喜欢

热点阅读