当前位置:网站首页>C language preprocessing instructions and makefile script explanation
C language preprocessing instructions and makefile script explanation
2022-07-26 03:25:00 【Programmer_ Xuyih】
C Language preprocessing instructions and Makefile Script explanation
One 、 Preprocessing instruction
The code written by programmers is not a real standard C Code , Need a program to translate into standard C Code , Can be compiled by the compiler
The process of translation is called preprocessing 、 The program responsible for translation is called The preprocessor 、 The translated sentence is called Preprocessing instruction , With # It starts with Preprocessing instruction
gcc -E code.c Directly display the results after pretreatment
gcc -E code.c -o code.i Generating preprocessing files
Two 、 Classification of preprocessing instructions
#include Import header file / The header file contains
#include <> Find and import header files from the path specified by the system
#include "" First search from the current working path , Unable to find and import the header file from the path specified by the system
gcc code.c -I path
When compiling, the loading path of the specified header file is path, Find the path first
You can modify... By setting environment variables 、 Add the system specified header file loading path modify ~/.bashrc Terminal configuration file
#define Defining macro
Macro constants :
#define Macro name Constant values
#define MAX 100
It's essentially , Where the macro name appears in the code , Replace with the corresponding constant value during preprocessing
advantage : Improve scalability 、 Improved readability 、 Improved security , It can also be used in case Back
Be careful : Generally, all macro names are capitalized , Don't put a semicolon at the end
local variable 、 Function names are all lowercase , Global variable capitalized array arr, The pointer p pp, character string str
Predefined macros :
__func__ Get function name
__FILE__ Get the file name
__DATE__ Get current date
__TIME__ Get the current time
__LINE__ Gets the current number of rows %d
Macro functions : It's actually a macro with parameters
#define Macro name ( Parameter name ) Replacement code
Macro functions are not real functions , Do not check the parameter type 、 There is no reference 、 Only the evaluation result of the expression has no return value
#define SUM(a,b) a+b
SUM(num1,num2)
Replacement process :
1、 First, replace the place where the macro function is used in the code with the code behind the macro function a+b
2、 Then replace the parameters used in the macro function code with the data provided by the caller
Be careful : The replacement code after a macro function cannot wrap directly , You need a continuation character at the end of each line \ To wrap
If there are multiple lines of code , You can protect your code with curly braces
Ambiguity of macro :
Due to the location of the macro code 、 Parameter position 、 Priority questions , It leads to different interpretable results of the same macro function
How to avoid the ambiguity of macros :
1、 Put parentheses on each parameter 、 Add parentheses to the whole formula
2、 When using macro functions , Do not provide parameters with automorphic operators
Be careful : Easy to make multiple-choice questions
Conditional compilation :
Determine which code will participate in the final compilation according to the conditions
version control :
#if Conditions
#elif Conditions
#else
#endif
Head file guard :
Prevent header files from being repeat contain
#ifndef Macro name
#define Macro name
#endif// Hongming
Macro name : Header file names are all capitalized , The decimal point is underlined
Be careful : Header file guard must be added to header file
debugging 、 Judge :
#ifdef Macro name
If the macro name is defined , Then the code here participates in compilation
#else
otherwise , Participate in the compilation here
#endif
gcc code.c -D Macro name
Macros can be defined at compile time
Define macro functions for printing debugging information , Whether to print prompt information , Depends on whether DEBUG macro
#ifdef DEBUG
#define debug(...) printf(__VA_ARGS__)
#else
#define debug(...)
#endif
Macro function for printing error messages
#define error(...) printf("%s:%s %d %s: %m %s %s\n",__FILE__,__func__,__LINE__,__VA_ARGS__,__DATE__,__TIME__)
3、 ... and 、 The header file
What should be written in the header file ?
problem : Header files may be included in any source file , It means that the contents of the header file will exist in multiple target files at the same time , Therefore, there should be no conflict in the merged content
a key : Only write declaration statements in the header file , There must be no defining statements
Function declaration
Global variable declaration
Macro constants
Macro functions
typedef Type redefinition
structure 、 union 、 Enumeration type declaration
The compilation principle of header file :
1、 For each .c Prepare a document with the same name .h file ,.h File pair .c Description of the document
2、 If you need to use a .c Global variables in the file 、 function 、 Macro and other contents , Just import its header file
3、.c Also import its own .h file , The purpose is to declare that it is consistent with the definition
Mutual inclusion between header files :
hypothesis a.h Contains b.h,b.h Also contains the a.h, At this time, the compilation will make an error
Solution : hold a.h Import required b.h Content and b.h Import required a.h Extract the content of to another c.h
Error message : Unknown type name "xxxx", High probability is that header files contain each other
Makefile
Makefile It is an executable file composed of a series of compilation instructions , Also called compilation script
Execute at the terminal make command , Automatically Makefile Compilation instructions in the script file , It can judge which files need to be recompiled according to the last modification time of the files 、 Which do not need to be recompiled , So as to improve the compilation efficiency
Compilation Rules :
1. If the project has not been compiled , So all of us c Files are compiled and linked .
2. If some of the projects c The document was modified , So we only compile the modified c file , And link the target program .
3. If the header file of this project is changed , Then we need to compile the file that references these header files c file , And link the target program .
One of the simplest Makefile Format :
Execution target : rely on
Compile instructions :
The goal to be depended on 1: Dependency file
Compile instructions
The goal to be depended on 2: Dependency file
Compile instructions
...
边栏推荐
- Etcdv3 actual combat (III) -prevkv description and related operations
- tf.truncated_normal()用法
- canvas——矩形的绘制——柱状图的制作
- LoRa无线网关如何快速实现端到云的传输
- What's good for starting a business with 10000 yuan? Is we media OK?
- 网络模型及协议
- UE4 how to render statically? 5 steps to generate static rendering
- leetcode-202.快乐数
- A 4W word redis interview tutorial
- tf.truncated_ Normal() usage
猜你喜欢

LeetCode·

Canvas -- draw text -- modification of pie chart

班级里有一群学生考试结果出来了,考了语文和数学两门,请筛选出总分是第一的同学

使用VRRP技术实现网关设备冗余,附详细配置实验

经典面试问题——OOP语言的三大特征

Managing databases in a hybrid cloud: eight key considerations

TCP experimental verification

ext4、ntfs、xfs、btrfs、zfs、f2fs和reiserFS性能对比

canvas——绘制曲线——挂钟,饼图,五角星

LeetCode·每日一题·919.完全二叉树插入器·层次遍历·BFS
随机推荐
Easyexcel sets row hiding to solve the problem of sethidden (true) invalidation
班级里有一群学生考试结果出来了,考了语文和数学两门,请筛选出总分是第一的同学
canvas——矩形的绘制——柱状图的制作
Sentinel vs Hystrix 到底怎么选?
MPLS basic experiment configuration
9-20v input peak charging current 3A dual lithium switch type charging chip sc7102
Etcdv3 actual combat (III) -prevkv description and related operations
Offline data warehouse from 0 to 1-stage II software installation
Unknown-Aware Object Detection:Learning What You Don’t Know from Videos in the Wild(CVPR 2022)
Classic interview questions -- three characteristics of OOP language
称霸薪酬榜!什么行业大有“钱”途?
Intensive reading of the paper -yolov1:you only look once:unified, real time object detection
tensorflow中tf.Variable()函数的用法
Efficient Video Instance Segmentation via Tracklet Query and Proposal
ELS window settings, WM_ CREATE、WM_ PAINT
Canvas - ECG design and how to clean the canvas
redis集群的三种方式
Looking at the next step of BAIC bluevale through the 8billion fund-raising, product upgrading and building core capabilities are the key words
Why did Mr. Cao, a productionist in the field of high-end tea, choose Ruifeng L6 max?
Uncaught TypeError: $(...).onmouseenter is not a function js错误,解决办法: