C code compiled into an executable procedure

Source: Internet
Author: User

C code compiled into executable code through the compiler, went through four stages, in order: preprocessing, compiling, compiling, linking.

The next step is to explain each stage in detail

First, pretreatment

1. Tasks: Macro definition expansion, header file expansion, conditional compilation, no check syntax.

2. Command: GCC-E [source file]-o [preprocess file]

3, Case: Using the GCC compiler preprocessing demo1.c code, pre-processed text into the demo1.i. (GCC-E demo1.c-o demo1.i)

The DEMO1.C code is as follows:

1#include <stdio.h>2 3 #defineAdd (A, b) (A + B)4 #defineSub (A, b) (a A)5 6 intMainvoid)7 {8     intA, B, C, D;9 #ifndef __cplusplusTenA = b = c = d =1; One #else AA = b = c = d =2; - #endif -printf"num =%d\n", Sub (add (A, B), add (c, D))); the     return 0; -}
demo1.c

The generated demo1.i code is as follows:

1#1 "demo.c"2#1 "<command-line>"3#1 "/usr/include/stdc-predef.h" 1 3 44#1 "<command-line>" 25 6 omit 800 lines here ...7 8 extern voidFunlockfile (FILE *__stream) __attribute__ ((__nothrow__, __leaf__));9#943 "/usr/include/stdio.h" 3 4Ten  One#2 "demo.c" 2 A  -  - intMainvoid) the { -     intA, B, C, D; -  -A = b = c = d =1; +  -  +  Aprintf"num =%d\n", ((A + B)-(C +( D) )); at     return 0; -}
demo1.i

The case can be found: #define宏定义, stdio.h header file, #ifdef条件编译都被替换了, and you can deliberately write a syntax error code, but do not error. Because the stdio.h header file is more than 800 lines long, only a few lines at the beginning and end of the demo1.i are truncated.

    

Second, compile

1, Task: Check the syntax, the pre-processed files compiled to generate assembly files.

2. Command: gcc-s [source file]-o [assembly file]

3, case; Compile the demo2.c code with the GCC compiler and put the compiled assembly code into DEMO2.S. (Gcc-s demo2.c-o Demo2.s)

The DEMO2.C code is as follows:

1 #include <stdio.h>23int main (intChar *argv[])  4{5     printf ("Hello world\n"); 6     return 0 ; 7 }
demo2.c

The generated DEMO2.S code is as follows:

1. file"demo2.c"2 . Section. Rodata3.LC0:4. String"Hello World"5 . Text6 . Globl Main7 . Type Main, @function8 Main:9.LFB0:Ten . Cfi_startproc One Pushq%RBP A. cfi_def_cfa_offset - -. cfi_offset6, - - - movq%rsp,%RBP the. cfi_def_cfa_register6 -SUBQ $ -,%RSP -MOVL%edi,-4(%RBP) -Movq%rsi,- -(%RBP) + MOVL $. LC0,%edi -     Pagerputs +MOVL $0,%eax A     Leave at. CFI_DEF_CFA7,8 -     ret - . Cfi_endproc -.LFE0: - . Size main,.-main -. ident"GCC: (Ubuntu 4.8.2-19ubuntu1) 4.8.2" in. section. Note. Gnu-stack,"", @progbits
Demo.s

We can see from the case that the C code we wrote was compiled into the assembly code. Intentionally write wrong A grammar point, the compiler will error.

III. compilation

1. Task: Generate the assembly file to the target file (2 binary file).

2. Command: gcc-s [source file]-o [target file]

3. Case: Compile demo3.c code with GCC compiler and put the compiled binary code into DEMO3.O. (Gcc-c demo3.c-o demo3.o)

The DEMO3.C code is as follows:

1 #include <stdio.h>23int main (intChar *argv[])  4{5     printf ("Hello world\n"); 6     return 0 ; 7 }
demo3.c

The generated DEMO3.O code is as follows:

Through the assembly phase, the text code becomes the binary code, which is the code that the computer can recognize. In the C language, the binary code file is suffixed with. O.

Iv. Links

1. Task: Locate the dependent library file and link the target file to the executable program.

2. Command: gcc-c [target file]-o [executable program]-l[Dynamic Library name]

3, Case: Through the GCC compiler let Demo4 link their own libadd.so dynamic library, and the Demo4 compiled into an executable program. GCC Demo4.c-o demo4-l./-ladd

The DEMO4.C code is as follows:

1#include <stdio.h>2#include"add.h"3 4 intMainintargcChar*argv[])5 {6printf"add =%d\n", Add (1,1));7     return 0;8}
demo4.c

To view the executable program information through the file command:

Run Result: add = 2

You can also view the size of the program's text segment, data segment, and BSS segment with the "Size [executable program]" command.

Before the program is run, we can determine the size of its text, data, and BSS segments.

C code compiled into an executable procedure

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.