Hello基本操作
准备工作
#include <stdio.h>
int main(void) {
printf("Hello World! \n");
return 0;
}
 预处理基本操作
- 将所有的#define 删除,并且展开所有的宏定义,并且处理所有的条件预编 译指令,比如#if #ifdef #elif #else #endif 等。
- 处理#include 预编译指令,将被包含的文件插入到该预编译指令的位置。
- 删除所有注释“//”和“/* */”。
- 添加行号和文件标识,以便编译时产生调试用的行号及编译错误警告行号。
- 保留所有的#pragma 编译器指令,后续编译过程需要使用它们。
 hello.i文件 
编译
将预处理生成的 hello.i 文件编译生成汇编程序 hello.s  
汇编
汇编过程调用对汇编代码进行处理,生成处理器能识别的指令,保存在后缀为.o 的目标文件中。 
将编译生成的 hello.s 文件汇编生成目标文件 hello.o GCC 的选项-c 使 GCC 在执行完汇编后停止,生成目标文件
链接
-
静态链接: 静态链接是指在编译阶段直接把静态库加入到可执行文件中去,这样可执行 文件会比较大。  text链接静态库,代码尺寸变得极大  没有链接动态库 -
动态链接: 动态链接则是指链接阶段仅仅只加入一些描述信息,而程序执行时再从系统 中把相应动态库加载到内存中去。 gcc hello.c -o hello   链接了许多其他的动态库,主要是linux系统glibc 动态库
ELF文件分析
链接器链接后生成的最终文件为 ELF 格式可执行文件,一个 ELF 可执行文件通常 被链接为不同的段,常见的段譬如.text、.data、.rodata、.bss 等段
.text:已编译程序的指令代码段。 .rodata:ro 代表 read only,即只读数据(譬如常数 const)。 .data:已初始化的 C 程序全局变量和静态局部变量。 .bss:未初始化的 C 程序全局变量和静态局部变量。 .debug:调试符号表,调试器用此段的信息帮助调试。  

反汇编ELF
由于 ELF 文件无法被当做普通文本文件打开,如果希望直接查看一个 ELF 文件包 含的指令和数据,需要使用反汇编的方法   使用 objdump -S 将其反汇编并且将其 C 语言源代码混合显示出来:  
gcc命令基本操作
简单编译

- 预处理:
gcc -E test.c -o test.i 或 gcc -E test.c gcc 的-E 选项,可以让编译器在预处理后停止,并输出预处理结果 - 编译为汇编代码
gcc -S test.i -o test.s  gcc 的-S 选项,表示在程序编译期间,在生成汇编代码后,停止,-o 输出汇编代码文件。 - 汇编
gcc -c test.s -o test.o - 链接
gcc 连接器是 gas 提供的,负责将程序的目标文件与所需的所有附加的目标文件连接起来,最终生 成可执行文件。附加的目标文件包括静态连接库和动态连接库。 gcc test.o -o test 
多个程序文件的编译
 gcc -c test1.c -o test1.o gcc -c test2.c -o test2.o gcc test1.o test2.o -o test
- 检错
a. gcc -pedantic illcode.c -o illcode -pedantic 选项能够帮助程序员发现一些不符合 ANSI/ISO C 标准的代码,但不是全部,事实上只有 ANSI/ISO C 语言标准中要求进行编译器诊断的 那些情况,才有可能被 GCC 发现并提出警告 b. gcc -Wall illcode.c -o illcode 使用它能够使 GCC 产生尽可能多的警告信息
总结
静态库链接时搜索路径顺序:
- ld 会去找 GCC 命令中的参数-L
- 再找 gcc 的环境变量 LIBRARY_PATH
- 再找内定目录 /lib /usr/lib /usr/local/lib 这是当初 compile gcc 时写在程序内的
动态链接时、执行时搜索路径顺序:
- 编译目标代码时指定的动态库搜索路径
- 环境变量 LD_LIBRARY_PATH 指定的动态库搜索路径
- 配置文件/etc/ld.so.conf 中指定的动态库搜索路径
- 默认的动态库搜索路径/lib
- 默认的动态库搜索路径/usr/lib
|