当前位置:网站首页>程序的编译和运行
程序的编译和运行
2022-07-25 20:32:00 【聪明的骑士】
目录
一、程序的翻译环境和执行环境
1.C语言代码运行需要存在两个环境
- 第1种是翻译环境,在这个环境中源代码被转换为可执行的机器指令。
- 第2种是执行环境,它用于实际执行代码。
2.一个C程序的编译运行过程
- 组成一个程序的每个源文件通过编译过程分别转换成目标代码(object code)。
- 每个目标文件由链接器(linker)捆绑在一起,形成一个单一而完整的可执行程序。
- 链接器同时也会引入标准C函数库中任何被该程序所用到的函数,而且它可以搜索程序员个人的程序库,将其需要的函数也链接到程序中

二、编译
编译分为预处理、编译和汇编三个过程
下面这个程序包含两个文件,我将详细讲解这个程序的编译过程
test.c
int Add(int a, int b)
{
return a + b;
}function.c
#define A 20
#include<stdio.h>
int b = 10;
int main()
{
int a = A;
int c = Add(a, b);
printf("%d", c);
//加法运算
return 0;
}1.预处理
详解预处理指令详见:(9条消息) 预处理指令_聪明的骑士的博客-CSDN博客
预处理会对代码先进行一系列操作方便后续编译,生成.i文件
程序在预处理阶段主要完成三个任务:头文件的包含、删除所有#define并替换所有的宏和标识符常量、删除所有条件编译指令、删除所有注释、添加行号和文件标识、保留#pragma指令
效果如下:
test.c
int Add(int a, int b)
{
return a + b;
}function.c
…… 此处为stdio.h的内容
#define被删除
int b = 10;
int main()
{
int a = 20;A被替换
int c = Add(a, b);
printf("%d", c);
下面的注释被删除
return 0;
}2.编译
将预处理后的代码进行词法分析、语法分析、语义分析、符号汇总以及后续优化产生相应的汇编语言,生成.s文件。

这里主要讲一下符号汇总,符号汇总主要就是把预处理代码中的函数名、全局变量统计出来做成一个表格。
test.o | function.o |
b(全局变量) | Add(函数名) |
main(函数名) | |
Add(函数名) |
等到一会儿链接的时候就可以通过这个表格检查函数是否正确定义
3.汇编
将汇编语言转化为二进制指令,此时生成的.o文件只有计算机可以看懂
最终生成test.o和function.o
三、链接
1.链接
链接阶段会进行合并段表和符号汇总两个操作从而形成一个可执行程序。
合并段表:编译器会把在汇编阶段生成的多个目标文件中相同格式的数据合并在一起,最终形成一个可执行程序。
在编译链接的过程中,文件后缀的变化是不一样的:
- 在windows系统中,文件的变化:.c -> .obj -> .exe(编译的三步一般一步到位)
- 在linux系统中,文件的变化:.c -> .i ->.s -> .o -> .out
2.符号汇总
在连个表中都存在Add函数名,链接器会自己确认哪一个位置是函数的定义,如果都不是,这个程序的编译就会出错。所以,链接是机器可以检查出函数是否没有定义的一步。
test.o | function.o |
b(全局变量) | Add(函数名)(经过检查这个是定义) |
main(函数名) | |
Add(函数名) |
四、程序的执行环境
程序执行的过程:
- 程序必须载入内存中。在有操作系统的环境中:一般这个由操作系统完成。在独立的环境中,程序的载入必须由手工安排,也可能是通过可执行代码置入只读内存来完成。
- 程序的执行便开始。接着便调用main函数。
- 开始执行程序代码。这个时候程序将使用一个运行时堆栈(stack),存储函数的局部变量和返回地址。
- 程序同时也可以使用静态(static)内存,存储于静态内存中的变量在程序的整个执行过程一直保留他们的值。
- 终止程序。正常终止main函数;也有可能是意外终止。
边栏推荐
- “链”接无限可能:数字资产链,精彩马上来!
- When AI encounters life and health, Huawei cloud builds three bridges for them
- JVM (XXIII) -- JVM runtime parameters
- PMP practice once a day | don't get lost in the exam -7.25
- 【TensorRT】动态batch进行推理
- Fanoutexchange switch code tutorial
- Apache MINA框架「建议收藏」
- 4. Server startup of source code analysis of Nacos configuration center
- 推荐系统专题 | MiNet:跨域CTR预测
- 参与开源社区还有证书拿?
猜你喜欢
![Summarize the level of intelligent manufacturing discussion [macro understanding]](/img/84/3addabdf857c562535a4085782d3e8.png)
Summarize the level of intelligent manufacturing discussion [macro understanding]

Remote monitoring solution of intelligent electronic boundary stake Nature Reserve

Myormframeworkjdbc review and problem analysis of user-defined persistence layer framework, and thought analysis of user-defined persistence layer framework

Online XML to JSON tool

毕业从事弱电3个月,我为什么会选择转行网络工程师
![[matlab] download originality documents based on oil monkey script and MATLAB](/img/c2/1788b758778ba73dd02fb0d006869e.png)
[matlab] download originality documents based on oil monkey script and MATLAB

Volcanic engine Xiang Liang: machine learning and intelligent recommendation platform multi cloud deployment solution officially released

「分享」DevExpress ASP.NET v22.1最新版本系统环境配置要求

火山引擎项亮:机器学习与智能推荐平台多云部署解决方案正式发布
![[today in history] July 5: the mother of Google was born; Two Turing Award pioneers born on the same day](/img/7d/7a01c8c6923077d6c201bf1ae02c8c.png)
[today in history] July 5: the mother of Google was born; Two Turing Award pioneers born on the same day
随机推荐
[today in history] July 5: the mother of Google was born; Two Turing Award pioneers born on the same day
4. Server startup of source code analysis of Nacos configuration center
[Infographics Show] 248 Public Domain Name
[workplace rules] it workplace rules | poor performance
RF、GBDT、XGboost特征选择方法「建议收藏」
LeetCode通关:哈希表六连,这个还真有点简单
[advanced mathematics] [1] function, limit, continuity
Chinese son-in-law OTA Ono became the first Asian president of the University of Michigan, with an annual salary of more than 6.5 million!
Vulnhub | dc: 6 | [actual combat]
JVM(二十三) -- JVM运行时参数
【高等数学】【6】多元函数微分学
JMeter - interface test
【TensorRT】动态batch进行推理
Online random coin tossing positive and negative statistical tool
Myormframeworkjdbc review and problem analysis of user-defined persistence layer framework, and thought analysis of user-defined persistence layer framework
[today in history] July 19: the father of IMAP agreement was born; Project kotlin made a public appearance; New breakthroughs in CT imaging
Network protocol: TCP part2
QQ是32位还是64位软件(在哪看电脑是32位还是64位)
10. < tag dynamic programming and subsequence, subarray> lt.53. maximum subarray and + lt.392. Judge subsequence DBC
Leetcode customs clearance: hash table six, this is really a little simple
