当前位置:网站首页>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
边栏推荐
- Understand chisel language thoroughly 08. Chisel Foundation (V) -- wire, REG and IO, and how to understand chisel generation hardware
- DGraph: 大规模动态图数据集
- Unittest中的TestSuite和TestRunner
- 1200. 最小绝对差
- 【Antd】Antd 如何在 Form.Item 中有 Input.Gourp 时获取 Input.Gourp 的每一个 Input 的value
- Install MySQL
- Doctoral application | West Lake University Learning and reasoning system laboratory recruits postdoctoral / doctoral / research internship, etc
- 2022 Shandong Province safety officer C certificate examination question bank and online simulation examination
- Understanding and difference between viewbinding and databinding
- Understand chisel language thoroughly 05. Chisel Foundation (II) -- combinational circuits and operators
猜你喜欢

Hardware Basics - diode Basics

CVPR 2022 | greatly reduce the manual annotation required for zero sample learning, and propose category semantic embedding rich in visual information (source code download)

Interview disassembly: how to check the soaring usage of CPU after the system goes online?

【R语言数据科学】:交叉验证再回首

One of the solutions for unity not recognizing riders

学内核之三:使用GDB跟踪内核调用链

MySQL version 8 installation Free Tutorial

测试流程整理(2)

Interviewer: what is the internal implementation of hash data type in redis?

JVM memory layout detailed, illustrated, well written!
随机推荐
[R language data science]: cross validation and looking back
如何在 2022 年为 Web 应用程序选择技术堆栈
markdown 语法之字体标红
吃透Chisel语言.12.Chisel项目构建、运行和测试(四)——Chisel测试之ChiselTest
Use the default route as the route to the Internet
Can mortgage with housing exclude compulsory execution
Automatic filling of database public fields
Understand chisel language thoroughly 03. Write to the developer of Verilog to chisel (you can also see it without Verilog Foundation)
Five "potential errors" in embedded programming
程序员的焦虑
2022年山东省安全员C证考试题库及在线模拟考试
吃透Chisel语言.09.Chisel项目构建、运行和测试(一)——用sbt构建Chisel项目并运行
392. Judgement subsequence
R语言使用lattice包中的bwplot函数可视化箱图(box plot)、par.settings参数自定义主题模式
Understand chisel language thoroughly 05. Chisel Foundation (II) -- combinational circuits and operators
BLOB,TEXT GEOMETRY or JSON column 'xxx' can't have a default value query 问题
Ws2811 m is a special circuit for three channel LED drive and control, and the development of color light strip scheme
Lick the dog until the last one has nothing (state machine)
IP 实验室月复盘 · 第 5 期
[matlab] summary of conv, filter, conv2, Filter2 and imfilter convolution functions