当前位置:网站首页>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
边栏推荐
- IDEA快捷键大全
- 学习项目是自己找的,成长机会是自己创造的
- Understand chisel language thoroughly 05. Chisel Foundation (II) -- combinational circuits and operators
- 软件测试之测试评估
- DGraph: 大规模动态图数据集
- xshell/bash/zsh 等终端鼠标滚轮乱码问题(转)
- 德明利深交所上市:市值31亿 为李虎与田华夫妻档
- Haobo medical sprint technology innovation board: annual revenue of 260million Yonggang and Shen Zhiqun are the actual controllers
- 测试流程整理(2)
- 2022 Shandong Province safety officer C certificate examination question bank and online simulation examination
猜你喜欢

如何在 2022 年为 Web 应用程序选择技术堆栈

【FAQ】華為帳號服務報錯 907135701的常見原因總結和解决方法

2022危险化学品经营单位主要负责人练习题及模拟考试

Test evaluation of software testing

基于YOLOv1的口罩佩戴检测

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

【Antd】Antd 如何在 Form.Item 中有 Input.Gourp 时获取 Input.Gourp 的每一个 Input 的value

Product identification of intelligent retail cabinet based on paddlex

Understand chisel language thoroughly 11. Chisel project construction, operation and test (III) -- scalatest of chisel test

Dgraph: large scale dynamic graph dataset
随机推荐
File creation, writing, reading, deletion (transfer) in go language
吃透Chisel语言.11.Chisel项目构建、运行和测试(三)——Chisel测试之ScalaTest
Gorm read / write separation (rotation)
華昊中天沖刺科創板:年虧2.8億擬募資15億 貝達藥業是股東
MySQL5免安装修改
xshell/bash/zsh 等终端鼠标滚轮乱码问题(转)
Unittest框架之断言
Summary of recent days (non-technical article)
FS4059C是5V输入升压充电12.6V1.2A给三节锂电池充电芯片 输入小电流不会拉死,温度60°建议1000-1100MA
Unity Shader学习(三)试着绘制一个圆
Deming Lee listed on Shenzhen Stock Exchange: the market value is 3.1 billion, which is the husband and wife of Li Hu and Tian Hua
Unittest中的TestSuite和TestRunner
QT how to detect whether the mouse is on a control
Gorm data insertion (transfer)
学内核之三:使用GDB跟踪内核调用链
IP 实验室月复盘 · 第 5 期
golang fmt.printf()(转)
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
Programmer anxiety
Understand chisel language thoroughly 08. Chisel Foundation (V) -- wire, REG and IO, and how to understand chisel generation hardware