15. compilation process
File. C, file. h goes throughPre-processorProcess it as file. I, and then go throughCompiler(GCC) to compile file. s, and thenAssembler(AS) becomes file. O, and finally passesConnector(Linker) becomes an executable file.
Pre-processor:1. process all comments and use spaces instead.
2. Delete all # define statements and expand all defined macros.
3. processing condition compilation command, # If, # ifdef, # Elif, # else, # endif
4. Process # include and expand the contained files
5. Retain the # pragma instruction required by the compiler
Command: gcc-e file. C-O hello. I.
Compiler: Perform a series of lexical analysis, syntax analysis, and Semantic Analysis on the pre-processed files. After the analysis, optimize the code and generate the corresponding assembly code.
Command: gcc-s file. C-O hello. s
Assembler: Transform assembly code into executable commands.
Command: gcc-C file. S-O hello. o
Connector: Process the referenced parts of each module so that the modules can be correctly connected. Static links are completed during the compilation period, and dynamic links are completed during the runtime.
16. macro definition and Usage Analysis
Macros can be defined anywhere in the code, and subsequent code can be used.
Macro expression and function comparison:
1. Macro expressions are processed in the pre-compiler (Simple ExpansionThe compiler does not know the existence of macro expressions.
2. Macro expressions use "real parameters" instead of form parameters. Without any operation, any character can be replaced and are widely used.
3. Macro expressions have no call overhead
4. Recursive definitions cannot appear in macro expressions.
Macro Ratio
An example of a macro that is stronger than a function:
# Define dim (array) (sizeof (array)/sizeof (* array) The first array represents the array int dim (INT array []) {return sizeof (array) /sizeof (* array); // The first array is a pointer}
In this example, the macro is replaced directly, so there is no problem,When the array name is used as a function parameter, it will directly degrade to a pointer.The returned value is incorrect (the number of BITs occupied by the pointer/the number of bits in the first element of the array, 32-bit returns 1, 64-bit returns 2 ).
Built-in macros:
17. Conditional compilation Usage Analysis
You can specify the macro definition used by the program during compilation to start preprocessing.-D defines the macro-u to cancel the macro.
# Include is essentially to embed the file content into the current file, so the contained file should use # ifndef ...... # Define ...... # Endif to prevent repeated inclusion
18. # error and # Line
# Error is used to generate a compilation error message and stop compilation.
Usage: # error message (double quotation marks are not required for message)
# Similar role of warning
# Line is used to forcibly specify the new line number and the name of the compiled file, and re-number the source code. The new line number starts from the next line. Used to specify your own code. Now you can use more advanced version control, static library, and dynamic library to solve multi-person encoding.
Usage: # line number filename (filename can be omitted)
19th. # pragma preprocessing Analysis
# Pragma is a compiler indicator used to indicate the compiler to complete certain actions.
# Many of the indicators defined by Pragma are unique to compilers and operating systems and cannot be transplanted between different compilers.
# Pragma: the pre-processor ignores unknown # pragma commands and deletes them, and leaves them to the compiler for processing.
# Pragma message: the message is output to the compilation output window during compilation, which can be used for code version control. It is unique to VC and will be ignored by GCC.
Memory alignment:
Why: the CPU does not read the memory continuously, but reads data in blocks. The size of the block can only be 1, 2, 4, 8, or 16 bytes.
When the data in the read operation is not aligned, two bus cycles are required to access the memory. Therefore, the performance is compromised.
Some hardware platforms can only retrieve certain types of data from specified addresses; otherwise, a hardware exception is thrown.
# The Pragma pack can change the default alignment of the compiler.
Struct memory usage calculation method:
By default, GCC is 4 bytes aligned.
The memcmp function cannot be used to determine whether the struct is equal. Because memcmp uses memory for determination, the struct is not cleared before use, even if the same value is assigned again, the values in each memory address are not necessarily the same. Another reason is that some memory addresses are undefined due to alignment.
20. # And # Operators
# The operator is the start character of the pre-processing command, for example, # include <>
# The operator is used to convert macro parameters to strings during pre-compilation.
#include <stdio.h>#define CONVERS(x) #xint main(){ printf("%s\n", CONVERS(Hello world!)); printf("%s\n", CONVERS(100)); printf("%s\n", CONVERS(while)); printf("%s\n", CONVERS(return)); return 0;}
Output result: Hello world!
100
While
Return
##Used to stick two symbols during pre-Compilation
#include <stdio.h>#define NAME(n) name##nint main(){ int NAME(1); int NAME(2); NAME(1) = 1; NAME(2) = 2; printf("%d\n", NAME(1)); printf("%d\n", NAME(2)); return 0;}
Output: 1
2
Preprocessor replaces name (1) with name1, name (2) with name2
Usage: defines the struct type.
#include <stdio.h>#define STRUCT(type) typedef struct _tag_##type type;\struct _tag_##typeSTRUCT(Student){ char* name; int id;};int main(){ Student s1; Student s2; s1.name = "s1"; s1.id = 0; s2.name = "s2"; s2.id = 1; printf("%s\n", s1.name); printf("%d\n", s1.id); printf("%s\n", s2.name); printf("%d\n", s2.id); return 0;}
Efficient and clean definition struct for Qualcomm platform.