当前位置:网站首页>02. Use of gcc compiler
02. Use of gcc compiler
2022-04-21 21:31:00 【anieoo】
One 、GCC The build process
One C/C++ The document goes through Preprocessing (preprocessing)、 compile (compilation)、 assembly (assembly) and link (linking) etc. 4 Step to become an executable file .

gcc The compiler can control the whole process by specifying different compilation options

(1) Preprocessing
C/C++ In the source file , With “#” The first command is called a preprocessing command , If it contains a command “#include”、 Macro definition command “#define”、 Conditional compilation command “#if”、“#ifdef” etc. . Preprocessing is about to include (include) Insert the file into the original file 、 Expand the macro definition 、 Select the code to use according to the conditional compilation command , Finally, output these things to a “.i” Waiting for further processing in the file .
(2) compile
Compiling is to put C/C++ Code ( Such as the above “.i” file )“ translate ” Into assembly code , The tools used are cc1( Its name is cc1,x86 Have their own cc1 command ,ARM The board also has its own cc1 command ).
(3) assembly
Assembly is to translate the assembly code output in the second step into machine code in a certain format , stay Linux The general performance of the system is ELF Target file (OBJ file ), The tools used are as.x86 Have their own as command ,ARM The version also has its own as command , It could be xxxx-as( such as arm-linux-as).
“ Disassembly ” It refers to converting machine code into assembly code , This is often used in debugging programs .
(4) link
The link will be generated in the previous step OBJ Of file and system libraries OBJ file 、 Linking library files , Finally, an executable file that can run on a specific platform is generated , The tools used are ld or collect2.
Two 、 Execute the whole compilation process in one step
The program code is as follows :
#include <stdio.h>
#define MAX 20
#define MIN 10
//#define _DEBUG
int main(int argc,char *argv[])
{
printf("Hello world\r\n");
printf("MAX = %d,MIN = %d,MAX + MIN = %d\r\n",MAX,MIN,MAX + MIN);
#ifdef _DEBUG
printf("Hello world\r\n");
printf("MAX = %d,MIN = %d,MAX - MIN = %d\r\n",MAX,MIN,MAX - MIN);
#endif
return 0;
}
Preprocessing :
gcc -E -o hello.i hello.c
It means that you will hello.c Document preprocessing , All information is stored in hello.i in .


Open generated hello.i, It is found that the header file to be included has been included in the preprocessing stage , The macro definition is expanded . Now we'll put the... In the program _DEBUG Macro definition open , It is found that the content of conditional compilation appears .

compile :
gcc -S -o hello.s hello.i
The preprocessed content can be compiled and generated by using the above command .s file , The contents of the document are as follows :

assembly :
gcc -c -o hello.o hello.s
Use the above command to .s Files can be programmed .o file , That is, the assembly process .
link :
gcc -o hello hello.o
The last step is to add no parameters , The system can be .o Documents and what you wrote .o The file is linked into executable APP
3、 ... and 、 Common compilation options
| Common options |
describe |
| -E |
Preprocessing , During the development process, you want to quickly determine that a macro can be used “-E -dM” |
| -c |
Pretreatment 、 compile 、 The compilation has been done , But don't link |
| -o |
Specify output file |
| -I |
Specify the header directory |
| -L |
Specify the link time library file directory |
| -l |
Specify which library file to link |
Usually use the command first :
gcc -c -o main.o main.c Command preprocessing 、 compile 、 The assembly is complete , Finally, use it by yourself gcc -o xxx.o Link .
Be careful :
gcc The compiler will not check your errors during and processing , It checks for errors in the compilation step .
版权声明
本文为[anieoo]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204212128120835.html
边栏推荐
- 还清90万安居贷才能拿到离职证明?腾讯回应:与实际情况不符
- Bailian3752 maze [BFS]
- Others - interface test by postman
- Qianxin Winter Olympics "zero accident" terminal security sharing meeting: 6641 attacks were successfully solved
- Others - use of siege pressure test tool
- 产品增长框架:从 Web2 到 Web3
- Product growth framework: from web2 to Web3
- Operation instructions of training management system
- Gan remake 2, which seems to be fun -- keras built srgan platform to improve the super-resolution of pictures
- 或许你不知道的12条SQL技巧
猜你喜欢

Let's take a look at this super comprehensive Android interview questions and analysis

CTF Crypto中涉及的AES题目

Kubernetes详解(五)——Kubernetes核心对象

AES topics involved in CTF crypto

After the virtual machine is shut down, restart Linux and restart Oracle monitoring inside

聪明的人脸识别1——Keras 搭建自己的Facenet人脸识别平台

“三战“港交所,“过气网红“绿茶餐厅讲不出新故事?

Reflex WMS系统,跟SAP系统的几个神似之处

32-bit and 64 bit computers

Jmeter(十六) - 从入门到精通 - JMeter前置处理器(详解教程)
随机推荐
常用sql优化
flutter插件第三方库,给Android程序员的一些面试建议
JVM 从入门到放弃之 ZGC 垃圾收集器精讲
聪明的人脸识别3——Pytorch 搭建自己的Facenet人脸识别平台
Others - Introduction to Devops
OA form design case display
[selenium self study series] (I) selenium's first example and interaction principle
Bailian4004 digit combination [recursion + DP]
返璞归真,多方安全计算要回归到“安全”的本源考虑
【zigbee无线通信模块步步详解】ZigBee3.0模块建立远程网络控制方法
Those things about SAP - Career - 36 - from the subject of "fixed assets liquidation"
32-bit and 64 bit computers
Other - use of Supervisor
Tgip-cn 038 registration | in depth analysis of Apache pulsar source code reading correct posture (I)
Use fluent to animate a color filter for photos
Fundamentals of reinforcement learning (I): multi armed bandit
CTF Crypto中涉及的AES题目
Multi tenant points system function list
安洵杯2021_Crypto_複現
Mongo geoNear query 中文模糊搜索在PHP中的使用