当前位置:网站首页>Compilation of program
Compilation of program
2022-07-25 18:17:00 【Swordsman】
Program environment and pretreatment
Translation environment and execution environment
ANSI C There are two environments
- Translation environment : The source code is converted into executable machine instructions .
- execution environment : Actually execute the code .
compile + link
Each source file of the program is translated into object code through compilation (.obj).
After each target file passes through the linker , Put together , Form an executable program .
compile : Including precompiling , compile , assembly
precompile / Preprocessing (.c->.i)
- The header file contains
- #define Defines the substitution of symbols
- Deletion of comments
compile (.i->.s) C Conversion of language code into assembly code
- Syntax analysis
- Lexical analysis
- Semantic analysis
- Symbol summary
assembly (.s->.o) Convert assembly code into binary instructions
- Form a symbol table
link (.o+ Link library ->.exe)(linux The environment is .elf)
- Merge segment tables
- Merging and relocating symbol tables
Predefine relevant knowledge
Predefined symbols
__FILE__ // Source file
__LINE__ // Line number
__DATE__ // Compile date
__TIME__ // Compile time
__STDC__ // follow ANSI C, The value is 1(gcc, clang)
#define Defined identifier
characteristic : It doesn't take up memory , Is a temporary symbol , After precompiling, this symbol no longer exists .
give an example
Grammatical form :
#define name stuff
#define M 10
#define unsinged int size_t
#define FOR_CYCLE for(;;)
#define CASE break;case
//stuff Too long , You can write in separate lines , Except for the last line , You need to add a continuation character after each line (\)
#define DEBUG_PRINT print("file:%s\tline:%d\t \ date:%s\ttime:%s\n", \ __FILE__, __LINE__, \ __DATE__, __TIME__)
#define When defining an identifier , Finally, it's best not to add ;, Because we end a sentence with a semicolon , In general , Extra semicolons will be treated as empty statements ; But in special cases , Will cause program errors .
#define Defined macro
Replace the parameter in the middle of the text , This implementation is called macro .
Declaration method
#define name(parament-list) stuff
What needs to be noted here is
nameMust be with the left parenthesis , Otherwise it will become#defineDefined identifier .In order to ensure that the operation is carried out according to the original intention , You must put a pair of parentheses around the macro definition and macro definition expression .
for example
#define SQUARE(x) ((x)*(x))
#define DOUBLE(x) ((x)+(x))
#define Define the rules involved in symbols and macros
- When calling a macro , Check the parameters first , If there is
#defineDefined symbols , Replaced first . - The replaced text is inserted into the original text position of the program ; To macro , The parameter name is replaced by a value .
# and ##
# The function of is to change a macro parameter into a string
give an example
#define PRINT(N) printf("the value of "#N" is %d\n", N)
## The function of is to synthesize the symbols on both sides into a symbol ( The identifier of the link must be legal )
give an example
#define name(list1, list2) list1##list2
int main()
{
int Goforever = 10;
printf("%d\n", name(Go, forever));
return 0;
}
Macro parameters with side effects
Variables are prefixed ++,--
After ++,-- Will have side effects
When macro parameters are defined more than one , At this time, if the parameter has side effects , Using macros can be dangerous .
Advantages and disadvantages of macros compared with functions
advantage :
- Macros do not need to create and destroy function stack frames and bring out return values , So macros are faster than functions .
- Because macros have no type checking , So macros are type independent .
- Macro parameters can have types , Function doesn't work .
shortcoming :
- Macro has no type check , So it's not rigorous enough .
- Because macros are replaced when they are precompiled , Debugging can only be carried out during operation , So macros cannot be debugged .
- If you don't put parentheses around the macro definition and the expression of the macro definition , There may be a problem of operator priority .
#undef
#undef Used to remove a macro definition .
#define M 10// Macro definition
#undef M // Remove macro definition
Conditional compilation
Conditional compilation is when Compile when the conditions we set are met , If the conditions are not met, give up one or a group of statements .
Conditional compilation instructions
1.
#if Constant expression
...
#end if
2. Multi branch conditional compilation
#if Constant expression
#elif Constant expression
#else
#endif
3. Judge whether it is defined
#if defined(symbol)
#ifdef symbol
#if !defined(symbol)
#ifndef symbol
The file contains
#include The instruction is used to inform the preprocessor when the instruction appears contain Specifies the contents of the file .
Alternative :
The preprocessor first deletes this instruction , Then replace with the corresponding file content .
The source file is actually compiled several times after it is included several times .
How to include header files :
- The local file contains
Put the file name in "" Inside , When looking for this file , First, look in the directory where the source file is located , If not found , Then go to the standard path to find .
#include "filename"
- The library file contains
Put the file name in < > Inside , When looking for this file , Only go to the standard path to find .
Avoid repeated references to header files
- Write... At the beginning of each header file
#pragma once - Write... At the beginning of each header file
#ifndef __TEST_H__
#define __TEST_H__
// The header file contains
#endif
边栏推荐
- Chapter 5 Basic Scripting: Shell Variables
- NC68 跳台阶
- Good news! Ruiyun technology was awarded the member unit of 5g integrated application special committee of "sailing on the sea"
- 408 Chapter 2 linear table
- Drawing PDF form (II) drawing excel form style in PDF through iText, setting Chinese font, watermark, logo, header and page number
- MySQL page lock
- SLA 、SLO & SLI
- Drawing PDF tables (I) drawing excel table styles in PDF and downloading them through iText (supporting Chinese fonts)
- Sequential storage structure, chain storage structure and implementation of stack
- MATLAB中join函数使用
猜你喜欢

What is the relationship between cloud fluidization and cloud desktop

Number two 2010 real test site

imx6 RTL8189FTV移植

Cve-2022-33891 Apache spark shell command injection vulnerability recurrence

Creation of unity Bezier curve

Cloud XR面临的问题以及Cloud XR主要应用场景

数二2010真题考点

Boomi won the "best CEO in diversity" and the "best company in career growth" and ranked among the top 50 in the large company category

Idea integrates common functions of SVN code management

Good news! Ruiyun technology was awarded the member unit of 5g integrated application special committee of "sailing on the sea"
随机推荐
Combined with GHS multi, use Reza E1 simulator to realize the simulation and debugging of Reza rh850 single chip microcomputer
Use of join function in MATLAB
Basic knowledge of documents
「数字安全」警惕 NFT的七大骗局
Use of C language cjson Library
Drawing PDF form (II) drawing excel form style in PDF through iText, setting Chinese font, watermark, logo, header and page number
GAN的详细介绍及其应用(全面且完整)
Taishan Office Technology Lecture: conversion relations of inch, centimeter, pound, pika, Ti, line, word line and pixel
UnitTest框架应用
Why the future of digitalization depends on 3D real-time rendering
虚拟偶像代言产品出问题谁负责?
Pan domain name configuration method
SLA 、SLO & SLI
Imx6 rtl8189ftv migration
Drawing PDF tables (I) drawing excel table styles in PDF and downloading them through iText (supporting Chinese fonts)
How many points did NPDP pass? How to pass with high scores?
BL602 开发环境搭建
Stm32f105rbt6 internal flash debugging
这是一张机器&深度学习代码速查表
Talking about Devops monitoring, how does the team choose monitoring tools?