当前位置:网站首页>Compilation and link of C language program
Compilation and link of C language program
2022-07-19 02:00:00 【Monastic -0323】
Preface :
Once the “hello word” Shocking world , Let countless apes flock . Now let me talk about my understanding of this note .
The process of the birth of notes
I take c/c++ give an example , Use the compiler as the panel , The linker is the string , Create a zither . Last use c The code waves the strings for everyone , Feel the birth of this wonderful note .
Detailed explanation of zither panel - compiler
The functional core of the compiler :“c The language code -->( precompile )–> Assembly code -->( Compile optimization )–> Machine code ”
1. precompile
Preprocessing instructions contain macro definitions , Conditional compilation , The header file contains , Special symbols, etc ; The compiler will first process the preprocessing instructions , abbreviation precompile , precompiled The output file in , Constant only ; Like numbers 、 character string 、 Definition of variables , as well as C Keywords of language , Such as main, if , else , for , while , { , } , + , - , * , \ wait .
Preprocessing :
#pragma #error#pragmaInstructions can be followed by parameters , And according to the parameter content , Number, etc , Its functions vary greatly . The most commonly used instruction is #pragma once, Used to prevent repeated references to header files .Macro definition :
#define 【SrcName】 【DesName】
Direct will DesName Replace with SrcName, For subsequent use of macros, use SrcName.Conditional compilation :
#if #else #elif #endif #ifdef #ifndef
Conditional compilation allows programmers to selectively compile code segments . therefore , The compiler will filter useless code segments according to conditional compilation statements , Do not participate in the compilation process ( Even if the error is not reported ).The header file contains :
*#include <> #include ""*
Use#includeThe main purpose of is to make different source programs use common variables , Avoid repeating definitions .<>It mainly includes “/ usr/ include” System files under the path .“”It mainly includes programmer's customized header files , The storage path is generally at the same level as the system path .Special symbols
line FILE
#line Will be replaced with line number ,FILE Replaced file name, etc ;
2. compile , Optimize
The precompiled output file is used as compilation , Optimization of the Input file . Through grammar , lexing , take c Code conversion to equivalent assembly code ;
compile
According to the preprocessed output file , Will conform to the grammar , Lexical statements are translated into equivalent assembly language ;Optimize
Compiler optimization is the most complex compilation stage , In terms of details , It will delete invalid code , Delete public expression , Reduce cycle strength and other ways to optimize code ; In general , Optimization can be divided into assembly code optimization and machine code optimization , The former is related to computers , The latter is closely related to the hardware structure .
3. assembly
Assembly process actually refers to the process of translating assembly language code into target machine instructions . The generated relocatable file consists of data segment and code segment . It can create an executable or shared object file through links .
Detailed explanation of zither strings - The linker
The linker associates machine language code with related files ( Symbol table parsing , relocation ), The process of generating executable files ; For example, a function or variable in a header file is called in a function , Just link that header file and its specific implementation file .
1. Static links
The linker : Copy the code in the object file to the final executable ;
Static link library : A collection of object codes ;
Static link is to copy the relevant code to the corresponding location of the source code , Participate in program generation .
2. Dynamic links
In the process of program generation , Only a few symbols required for normal compilation are provided . During program execution , Mapping all contents in the dynamic link library to the address space of the corresponding process , Ensure the normal execution and calling of the program .
Zither playing - Note birth
The four processes of note birth are summarized as follows : Precompiling (.c) —> compile 、 The optimizer (.s、.asm)—> Sink programming (.obj、.o、.a、.ko) —> Link program (.exe、.elf、.axf etc. ). Understanding of the compilation process , I'm still at the threshold , Need to learn more . Welcome to consult each other , Common progress
about GCC compiler , The compilation instructions corresponding to the above process are as follows :
| name | GCC Instructions | result |
|---|---|---|
| c Language | nothing | *.c |
| precompile | -E | *.i |
| compile | -S | *.s |
| assembly | -C | *.o |
| link | -ld | *.exe |
边栏推荐
猜你喜欢

动手学深度学习--多层感知机篇(MLP)
【Go语言】代码覆盖测试(gcov)

FS32K148调试之WDOG与电源模式

【文献阅读】VAQF: Fully Automatic Software-Hardware Co-Design Framework for Low-Bit Vision Transformer

Frustratingly Simple Few-Shot Object Detection

电解电容特性及应用要点
![[literature reading] counting integer points in parametric polymers using barvinok's rational functions](/img/a2/3e1b248c7cd853ffea7a835111db65.png)
[literature reading] counting integer points in parametric polymers using barvinok's rational functions

静态库与动态库

Détection de bord de deuxième ordre laplacien de guassian Gaussian laplacien Operator

ROC 曲线讲解 (Receiver Operarating Curve)
随机推荐
【白话模电1】PN结与二极管
【文献阅读】MCUNet: Tiny Deep Learning on IoT Devices
Fairness in Semi-supervised Learning: Unlabeled Data Help to Reduce Discrimination
判断两个数组是否完全相等
【文献阅读】VAQF: Fully Automatic Software-Hardware Co-Design Framework for Low-Bit Vision Transformer
Show Me the Code之MXNet网络模型(三)
PCA主成分分析(降维)过程推导
Determine whether two arrays are exactly equal
Fair Multiple Decision Making Through Soft Interventions
HRNet
S32K148EVB 关于ENET Loopback实验
[go language] detailed explanation of dynamic library and static library
【文献阅读】Small-Footprint Keyword Spotting with Multi-Scale Temporal Convolution
SAE J1708/J1587 协议详解
windwos 下载安装OpenSSH
C语言运算符优先级
ACE下载地址
Ace download address
06基于STM32的智能电子药盒设计
14:07:08 ckati failed with: signal: killed