当前位置:网站首页>GCC【6】- 编译的4个阶段
GCC【6】- 编译的4个阶段
2022-07-04 12:50:00 【大城市的小蜗牛】
前言
GCC 编译一共分4个阶段:预处理、编译、汇编、链接
gcc 指令一般格式
gcc 【选项】要编译的文件【选项】【输出文件】
| 选项 | 说明 |
|---|---|
| -E | 控制GCC编译器仅对源码做预处理操作 |
| -S | 控制GCC编译器仅对指定文件处理之编译阶段 |
| -c | 控制GCC编译器仅对制定文件处理至汇编阶段,并生成相应的目标文件 |
| -o outfile | 指定输出文件的文件名 |
eg:下面是一段很简单的hello.c代码,以这段代码为例
#include <stdio.h>
#define NAME "mao"
void main()
{
printf("hello:%s\n",NAME);
}
一、预处理阶段
预处理阶段主要工作是删除程序中所有的注释、处理以#开头的命令。如:头文件的展开、宏定义的替换。
对于上面的代码#include <stdio.h>表示引入头文件,在预处理阶段stdio.h文件就会被加载到我的hello.c中,而对于在代码中使用到的NAME,就会被替换成该宏所定义的真实内容mao,可以使用下面的命令执行预处理:生成.i文件,linux 系统中预处理后所得文件的后缀名通常是.i
gcc -E hello.c -o hello.i
二、编译阶段
gcc在编译阶段会对预处理后的代码进行各种检查(词法分析,语法分析,语义分析),来判断我们的代码是否符合规范,如果通过生成汇编代码(.s结尾的文件)
注意:gcc -S不是只能编译经过预处理后的.i文件,它的含义是控制GCC编译器仅对指定文件处理至编译阶段
- 如果需要编译的文件是经过预处理后的
.i文件,则GCC编译器只需编译此文件。 - 如果需要编译的文件是
.c或者.cpp源文件,则GCC编译器会对其进行预处理和编译(两步)。
gcc -S hello.i -o hello.s
三、汇编阶段
把.s文件翻译成二进制.o文件(目标文件)
注意:gcc -c 不是只能编处理.s文件,它的涵义是控制GCC 编译器仅对指定文件处理至汇编阶段
- 如果指定文件为
.c文件,则gcc -c指令会对该文件执行预处理、编译、汇编 。 - 如果指定文件为经过预处理后的
.i文件,则gcc -c指令对该文件执行编译和汇编。 - 如果指定文件为经过编译后的文件,则
gcc -c指令只对该文件执行汇编操作。
gcc -c hello.s -o hello.o
四、链接阶段
汇编阶段将代码编译成了二进制文件,还需要和系统其他组件(比如标准库、动态链接库等)结合起来才能正常运行,比如调用print函数打印,在预处理阶段也只是将“stdio.h”头文件中的申明引入进来,没有函数的实现,那怎么调用它的呢?
这就是链接的工作,链接之前都是对文件的处理,而链接是对多个文件进行打包的过程,他将所有目标文件以及系统组件组合成一个可执行文件。
gcc hello.o -o hello
边栏推荐
- Worried about "cutting off gas", Germany is revising the energy security law
- 392. 判断子序列
- Basic mode of service mesh
- Unittest框架中引入TestFixture
- sharding key type not supported
- QT how to detect whether the mouse is on a control
- 2022年起重机械指挥考试模拟100题模拟考试平台操作
- Read excel table data
- 基于YOLOv1的口罩佩戴检测
- Secretary of Homeland Security of the United States: domestic violent extremism is one of the biggest terrorist threats facing the United States at present
猜你喜欢

吃透Chisel语言.09.Chisel项目构建、运行和测试(一)——用sbt构建Chisel项目并运行

sharding key type not supported
![[antd step pit] antd form cooperates with input Form The height occupied by item is incorrect](/img/96/379d1692f9d3c05a7af2e938cbc5d7.png)
[antd step pit] antd form cooperates with input Form The height occupied by item is incorrect

【Antd踩坑】Antd Form 配合Input.Group时出现Form.Item所占据的高度不对

How to choose a technology stack for web applications in 2022

Huahao Zhongtian sprint Technology Innovation Board: perte annuelle de 280 millions de RMB, projet de collecte de fonds de 1,5 milliard de Beida Pharmaceutical est actionnaire

Automatic filling of database public fields

2022年起重机械指挥考试模拟100题模拟考试平台操作

sharding key type not supported

英视睿达冲刺科创板:年营收4.5亿 拟募资9.79亿
随机推荐
The Secretary of Homeland Security warned immigrants "not to embark on a dangerous journey"
[antd] how to set antd in form There is input in item Get input when gourp Value of each input of gourp
瑞吉外卖笔记
Migration from go vendor project to mod project
MySQL 45 lecture - learn the actual combat notes of MySQL in Geek time 45 lecture - 06 | global lock and table lock_ Why are there so many obstacles in adding a field to the table
常见 content-type对应表
Can mortgage with housing exclude compulsory execution
FS4059C是5V输入升压充电12.6V1.2A给三节锂电池充电芯片 输入小电流不会拉死,温度60°建议1000-1100MA
美国土安全部部长警告移民“不要踏上危险的旅程”
Programmer anxiety
BLOB,TEXT GEOMETRY or JSON column 'xxx' can't have a default value query 问题
go语言中的文件创建,写入,读取,删除(转)
The mouse wheel of xshell/bash/zsh and other terminals is garbled (turn)
硬件基础知识-二极管基础
How to choose a technology stack for web applications in 2022
Use the default route as the route to the Internet
[FAQ] summary of common causes and solutions of Huawei account service error 907135701
File creation, writing, reading, deletion (transfer) in go language
Gorm 读写分离(转)
Dgraph: large scale dynamic graph dataset