Write in the opening: detailed introduction of the use of GCC/G ++ under the one -hal linux, collect it ~~
The text starts @Assassin
Directory:
- linux compiler-GCC/G ++ use:
-
- 1. Background knowledge:
- 2. How to complete GCC:
- 3. Compilation process:
- 4. Pre -processing (Macro replacement):
- 5. Compilation (generating assembly):
- 6. Compilation (generate a machine can identify code):
- 7. Connect (generate executable file or library file):
- 8. Here is an important concept-> Functional Library:
- 9. GCC/G ++ option:
- 10. GCC/G ++ option memory:
- Write at the end:
- see you next blog~~
- Pre -processing (Macro replacement)
- Compilation (generating assembly)
- assembly (generate machine can identify the code)
- connection (generate executable file or library file)
Format GCC [Options] The file to be compiled [Options] [target file]
- Preparatory processing: head file expand, macro replacement, annotation remove
gcc -E hello.c -o hello.i
- compilation: C file becomes an assembly file
gcc -S hello.i -o hello.s
- Compilation: Turn the assembly file into a binary file
gcc -c hello.s -o hello.o
- Link: Combining the corresponding code in the function library into the target file
gcc hello.o -o hello
Note:CorrespondenceCPPIn the compilation, you can replace the GCC with G ++ ~~
- Pre -processing function mainly includes macro definition, files, conditional compilation, comments, etc.
- Pre -processing instructions are code -based code.
- Example: gcc –e hello.c – O hello.i
- Options “-E”, the role of this option is to allow GCC to stop the compilation process after pre-processing.
- Options “-O” refers to the target file, and the “. I” file is the original C original program that has been pre-processing.
- In this stage, GCC must first check the normality of the code, whether there are grammatical errors, etc. to determine the actual work of the code. After checking, the GCC translates the code into an assembly language.
- Users can use the “-S” option to view. This option is only compiled without assembly, and the assembly code is generated.
- Example: GCC -S hello.i – O hello.s
The
- compilation phase is the “.s” file generated by the compilation stage into a target file
- Everyone can use the option “-c” here to see that the assembly code has been converted into the binary target code of “o “
- Example: GCC -C Hello.s – O Hello.o
- After successful compilation, it entered the link stage.
- Example: GCC Hello.o – O Hello
- In our C programs, there is no functional implementation of “Printf”, and only the “stdio.h” contained in the pre -compilation is only the statement of the function, and there is no degrading function implementation. So where is it, where is it? What about the “Printf” function?
The last answer of
- is: The system implements these functions in the library file named libc.so.6. When there is no special specification, the GCC will go to the system’s default search path “/usr/lib” Search below, that is, the link is connected to the libc.so.6 library function, so that the function “printf” can be achieved, and this is the role of link.
- Static library refers to adding the code of the library file to the executable file when compiling the link, so the generated files are relatively large, but the library file is no longer needed at runtime. The essence name is generally “.a”.
- Dynamic library is the opposite. When compiling the link, the code of the library file is not added to the executable file, but the library is loaded by the runtime link file during the program execution, which can save system overhead. The general suffix of the dynamic library is “.SO”, and the libc.so.6 as mentioned earlier is the dynamic library.
GCC uses the dynamic library by default during compilation. After completing the link, GCC can generate executable files, as shown below: GCC Hello.o – O Hello.The binary program generated by
- GCC is a dynamic link, which can be verified by the file command.
-E
Only activate pre -processing. This does not generate files. You need to redirect it into an output file.-S
Compilation to the assembly language does not compile and link.-c
Compile to the target code.-o
File output to the file.-static
This option uses a static link for the generated file.-g
Generate debugging information. The GNU debugger can use this information.-shared
This option will try to use the dynamic library as much as possible, so the generating files are relatively small, but the system needs to be used by the dynamic library.-O0
-O1
-O2
-O3
Optimized options for the compiler, -O0 means that there is no optimization, -O1 is the default value, and -O3 optimization level is the highest.-w
No warning information.- –
W
only generates information that the compiler thinks it will report an error. - –
Wall
generate all warning information. -i + path
Provide the header path of the required header during compilation.
My memory skills are: when compiling the option, you can think of on the keyboardESCkey, the exit key in the upper left corner,-E,-S,-c, is it very spiritual [DOGE], and then the files generated in turn can think of the provided by you to download the Linux system.isomirror file, HHH, in turn.i—–.s—–.o. It is estimated that you will forget HH for a while and a half —–
If you have any questions, please be issues ~~, thank you Daga^_^