To tell the truth, a lot of people have done a long time C + +, but also use a lot of Ides, but for the implementation of the underlying generation of a blank, this is undoubtedly a sad, you can imagine the big company interview just be asked this question, how sad urge self-evident, here is due to change work, So I intend to use the system to make a supplement to the C + +. Here right vote as a point, the great God floated.
"General statement"
From a source file (. c) to an executable program in the end, I think most people know what to do at every step, and I guess not many people can say it clearly, plainly.
In fact, the overall process is this.
"First step" edit hello.c
1 #include <stdio.h>2 #include <stdlib.h>3int main ()4 {5 printf ("hello world!\n"); 6 return 0 ; 7 }
"Second Step" preprocessing
The preprocessing process essentially deals with "#", copying the header files of # include directly into the HELL.C, replacing the macros defined by the # define, and removing the unused comment portions of the code, etc.
The specific thing to do is as follows:
(1) Delete all # define and expand all macro definitions. The white one is character substitution.
(2) handle all conditional compilation instructions, #ifdef #ifndef #endif等, that is, those with #
(3) To process # include, insert the file with the # include point at the line
(4) Delete all comments
(5) Add line numbers and file markers, so that when debugging and compilation error, you will know which file is which line
(6) Keep #pragma compiler directives because the compiler needs to use them.
gcc-e hello.c-o A.C can generate pre-processed files. By viewing the contents of the file and the size of the file you can tell that A.C stdio.h and stdlib.h included.
"Step Three" compilation
The process of compiling is essentially the process of translating high-level languages into machine languages, that is, doing these things to A.C.
(1) Lexical analysis,
(2) Grammatical analysis
(3) Semantic analysis
(4) optimize the corresponding assembly code
Machine language (binary) from high-level languages, assembly language
gcc-s hello.c-o A.S can generate assembly code
The assembly code is as follows.
1. file"hello.c"2 . Section. Rodata3 . LC0:4.string "Hello world!"5 . Text6 . Globl Main7 . Type Main, @function8 Main:9 . LFB0:Ten . Cfi_startproc OnePushl%EBP A. cfi_def_cfa_offset8 -. cfi_offset5, -8 -MOVL%esp,%EBP the. cfi_def_cfa_register5 -Andl $- -, %ESP -SUBL $ -, %ESP -MOVL $. LC0, (%ESP) + Call puts -MOVL $0, %eax + Leave A. cfi_restore5 at. CFI_DEF_CFA4,4 - ret - . Cfi_endproc - . LFE0: -. size main,.-Main -. ident"GCC: (Ubuntu/linaro 4.6.3-1ubuntu5) 4.6.3" in. section. Note. Gnu-stack,"", @progbits
gcc-c hello.c-o a.o translates the source file into a binary file. Class Uinx The result of a system compilation generates an. o file, and the Windows system generates an. obj file.
The process of compiling is to translate hello.c into binary files.
"Fourth Step" link
Just like the hello.c. It uses the C standard library of Things "printf", but the compilation process only translates the source file into binary, this binary is not directly executed, this time need to do an action,
The binary that will be translated is bound together with the library that needs to be used. For example, the process of compiling to you said to your wife, I want to eat ice cream. You just gave your wife the desire to eat ice cream, but the ice cream hasn't arrived yet.
The binding is to say you want to eat the ice cream your wife has bought you, you can happy.
gcc hello.c-o A can generate executable programs. That is, GCC does not take any parameters. LDD can see the libraries that your executable program relies on.
You can see that the size of the A.O is 1.1k, after all he just translates the source file into a binary file. A but there is 7k, he should be a lot more "rope" it. These "ropes" will "pull" the corresponding library function at the time of operation. It's an image metaphor, isn't it? Ha ha. The printf we used was defined in the libc.so.6.
This is the whole process of writing, (⊙o⊙). Thank you, crossing. Please do not hesitate to advise me of any deficiencies.
C-language Real compilation process