GCC 编译过程

一个 C/C++文件要经过预处理(preprocessing)、编译(compilation)、汇编(assembly)和链接(linking)等 4 步才能变成可执行文件。
在这里插入图片描述

体验一下上述的过程

#include <stdio.h>

#define   MAX  20 
#define   MIN  10 

//#define  _DEBUG
#define   SetBit(x)  (1<<x) 

int main(int argc, char* argv[])
{
    printf("Hello World \n");
    printf("MAX = %d, MIN = %d, MAX + MIN = %d\n", MAX, MIN, MAX + MIN); 

#ifdef _DEBUG
    printf("SetBit(5) = %d,SetBit(6) = %d\n",SetBit(5),SetBit(6));
    printf("SetBit( SetBit(2) ) = %d\n",SetBit( SetBit(2) ));		
#endif    
	return 0;
}

gcc -o hello hello.c -v

预处理:(查找头文件,展开宏) .c -> .i

1.查找头文件

#include “…” search starts here:
#include <…> search starts here:
/usr/lib/gcc/x86_64-linux-gnu/7/include
/usr/local/include
/usr/lib/gcc/x86_64-linux-gnu/7/include-fixed
/usr/include/x86_64-linux-gnu
/usr/include

编译:.i -> .s

cc1 hello.c -o /tmp/ccuVv7YJ.s

汇编:.s -> .o

as -v --64 -o /tmp/cceDi2sF.o /tmp/ccuVv7YJ.s

链接

把各种 .o 文件链接成 .elf

collect2 -o hello /tmp/cceDi2sF.o

可以手工执行以下命令体验一下:
预处理:

gcc -E -o hello.i hello.c

编译:

gcc -S -o hello.s hello.i

汇编:

gcc -c -o hello.o hello.s

链接:

gcc -o hello hello.o

注意:
语法错误是在编译过程中发现的,预处理过程中并未发现错误。

常见的编译选项

常用选项描述
-E预处理,开发过程中想要快速确定某个宏可以使用 “-E -dM”
-c把预处理,编译,汇编都做了,但是不链接
-o指定输出文件
-l指定头文件目录
-I指定链接哪一个库文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值