当前位置:网站首页>C language compilation process
C language compilation process
2022-07-19 09:13:00 【Not bald but also strong】
C The process of compiling a language :
Preprocessing 、 compile 、 assembly 、 link
gcc -E hello.c -o hello.i 1、 Preprocessing
gcc -S hello.i –o hello.s 2、 compile
gcc -c hello.s -o hello.o 3、 assembly
gcc hello.o -o hello_elf 4、 link
1: precompile
take .c Header file expansion in 、 Macro expansion The generated file is .i file
2: compile
The pretreated .i File generation .s Assembly files
3、 assembly
take .s Assembly file generation .o Target file
4、 link
take .o Link the file to the target file
include
#include<>// Include header files in angle brackets , Find the header file in the path specified by the system
#include "" // Include the header file in double quotation marks , First find the header file in the current directory , Can't find , Then go to the... Specified by the system Find... Under the path .
Be careful :include Often used to include header files , Can contain .c file , But don't include .c
because include The included files will be expanded after precompiling , If one .c Contained multiple times , Expand multiple times , It will cause the function to be defined repeatedly . So don't include .c file .
Be careful : Pretreatment is just for include Wait for preprocessing operations to be processed without syntax checking At this stage, there is no syntax error and no error will be reported , In the second stage, the compilation stage, syntax checking is carried out .
define
Define macros with define To define
Macros are replaced during precompiling .
1、 Macro without parameters #define PI 3.14
When precompiling, if the code appears PI Just use 3.14 Replace .
The benefits of macros : Just modify the macro definition , Other places will be replaced when precompiled . Be careful : Do not add a semicolon after the macro definition .
#include <stdio.h>
// The advantage of macro definition is that as long as you change the constant expression , Then the position defined by this macro in the code is
Will change
#define PI 3.1415926
int main(int argc, char *argv[])
{
printf("PI = %lf\n", PI);
double d = PI;
printf("d = %lf\n", d);
return 0;
}
Execution results

Scope of macro definition , From where defined to the end of this document .
If you want to terminate the definition scope of the macro in the middle
#undef PI // End PI The role of
2、 With parameters
#define S(a,b) a*b
Note the formal parameters of the macro with parameters a and b No type name ,
S(2,4) In the future, it will be replaced by The argument replaces the formal parameter of the string , Other characters are reserved ,2 * 4.
#include <stdio.h>
// With parameters
// A macro with parameters is similar to a simple function , Set the parameters of the function , You can pass it to the corresponding expression
//#define S(a, b) a*b
#define S(a, b) ((a)*(b))
int main(int argc, char *argv[])
{
printf("%d\n", S(2, 4));
// Be careful : The macro definition is just a simple replacement , No automatic parenthesis
// With parameters 1:2 + 8 * 4 = 34
// With parameters 2:((2 + 8) * (4)) = 40
printf("%d\n", S(2 + 8, 4));
return 0;
}
3、 The difference between a macro with parameters and a function with parameters
The macro with parameters will expand as many times as it is called , There is no function call procedure when executing code , There is no need to press Stack bomb stack . So take reference macro , It's a waste of space , Because it has been expanded many times , Save time .
Functions with parameters , There's only one copy of the code , There are code snippets , When calling, go to the code segment to get instructions , When calling, press Stack bomb stack . There is a procedure called . So , Functions with parameters are a waste of time , Save space .
Formal parameters of functions with parameters are typed , A formal parameter with a parameter macro has no type name .
If the code of function implementation is relatively simple , And don't need to open up too much space , You can choose to use macros with parameters , but In most cases, functions are used .
4、 Selective compilation
1、
#ifdef AAA
// Code snippet one
#else
// Code snippet two
#endifIf at present .c ifdef Defined above AAA , Just compile the code segment , Otherwise, compile code segment 2
Pay attention to and if else The difference between sentences ,if else Statements will be compiled , Execute code selectively through conditions and Selective compilation , Only one piece of code is compiled
#define AAA
int main(int argc, char *argv[])
{
#ifdef AAA
printf("hello kitty!!\n");
#else
printf("hello Qian Feng edu\n");
#endif
return 0;
}
2、
#ifndef AAA
// Code snippet one
#else
// Code snippet two
#endifComplementary to the first .
This method , It is often used to prevent header files from repeatedly containing .
Commonly used in multi file programming .h The first line is #ifndef, The last line is #endif.
3、
#if expression
// Program segment 1
#else
// Segment II
#endif
// If the expression is true , Compile the first piece of code , Otherwise, compile the second piece of code This form is generally used to annotate multiple lines of code #if 0 ... #endifSelective compilation is all done in the precompile phase .
边栏推荐
- 【Flink】Flink 设置检查点失败一次就报错 setTolerableCheckpointFailureNumber 不起作用
- How to build your own cloud database in docker
- 软件测试需要学习多久?
- 封装API,request拦截,response拦截,身份验证时效性
- It's also very difficult. I'm not only tired of writing by myself
- Cocos shader basics 7
- 【性能优化方法论系列】六、总结
- 银河麒麟v10-arm版离线安装Portainer
- 【C语言-自定义类型】还能这样整?
- [paper notes] visual detection and capture method based on deep learning
猜你喜欢
随机推荐
Qprocess of QT
SQL优化
Project contract analysis from the perspective of Software Engineer
力扣(LeetCode)197. 上升的温度(2022.07.16)
TP5 wechat withdrawal merchants transfer to change (copying is available)
How is MySQL data stored on disk?
How long does software testing take?
MySQL initialization and password modification
05---增透膜
codeforces每日5题(均1500)-第十七天
Qt之Qprocess
Microservice splitting for stand-alone projects
如何快速计算生成模型的FID、IS、sFID、Precision、Recall等关键评价指标?
Zero basic C language
Scope and lifecycle of beans
日志脱敏-参考
简易的第三方组件日志脱敏
Solve interface cross domain problems and node operation MySQL
Es conceptual model and basic faults
认真工作后,发现周围混事的真多









