The whole process of compiling C programs under Linux

Source: Internet
Author: User

Learn a language program, I think still have to learn its compiling rules, now, through the small examples of their own C compilation of understanding.

?
12345678 /*test.c   了解C程序的编译*/#include <stdio.h>int main(void){ printf("Hello World!\n"); return0;}

For test.c, we often use one step to compile the commands that are in place:

?
1 gcc -o testtest.c 或者 gcc test.c -o test

In fact, the above compilation command contains four stages of processing, namely preprocessing (also known as precompilation, preprocessing), compilation (compilation), assembly (Assembly), and connection (linking).

Here is a detailed listing of the complete compilation process

Pretreatment:

Role: The role of preprocessing is to read into the source code, check the statement and macro definition containing the preprocessing directives, and the transformation of the source code response. The preprocessing process also removes comments and extra white-space characters from the program.

Object: The preprocessing directives begin with "#", and the preprocessing objects mainly include the following:

(1) #define MACRO definition

(2) #运算符 #运算符作用是把跟在其后的参数转换成一个字符串.

?
12345678 /***例***/#define PASTE(n) "adhfkj"#nintmain(){   printf("%s\n",PASTE(15));   return0;}/********输出adhfj15*********/

(3) # #运算符 # #运算符的作用用于把参数连接到一起.

?
12345678910   /*****例*****/  #define NUM(a,b,c) a##b##c  #define STR(a,b,c) a##b##c  int main()   {     printf("%d\n",NUM(1,2,3));     printf("%s\n",STR("aa","bb","cc"));     return 0;   }/*********最后程序的输出为:aabbcc**********/

(4) Conditional compilation instructions

(5) header file contains instructions

(6) Special symbols

__file__ string containing the current program file name

__line__ An integer that represents the current line number

__date__ string containing the current date

__TIME__ contains the current string

As the pre-processing instructions for the test.c file above are

?
1 gcc -E test.c -o test.i

Compile-compile into assembly language

?
1 gcc -S test.i -o test.s

This is what the above code compiles TEST.S.

?
1234567891011121314151617181920212223242526 .file "test.c" .section .rodata.LC0: .string "hello world" .text.globl main .type main, @functionmain:.LFB0: .cfi_startproc pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 movq %rsp, %rbp .cfi_def_cfa_register 6 movl $.LC0, %edi call puts movl $0, %eax leave .cfi_def_cfa 7, 8 ret .cfi_endproc.LFE0: .size main, .-main .ident "GCC: (GNU) 4.4.7 20120313 (Red Hat 4.4.7-4)" .section .note.GNU-stack,"",@progbits

Assembly

Role: Compile the above assembly instructions to generate the target file

?
1 gcc -c test.s -o test.o

This is the contents of the TEST.O file above

?
123 ELF > 8 @ @  UH夊? ? ? 擅 hello world GCC: (GNU) 4.4.7 20120313 (Red Hat 4.4.7-4) zR x ? A?C P .symtab .strtab .shstrtab .rela.text .data .bss .rodata .comment .note.GNU-stack .rela.eh_frame @ ? 0 & X , X 1 X 9 0 d - B ? W ? 8 R ? ? a x € ? test.c main puts  ?

Link

The main purpose of the link is to link the program's target file to the desired attached target file, resulting in an executable file. The attached target file also includes the required library files (static link libraries and dynamic link libraries)

?
1 gcc test.o -o test

The resulting test file is the file that the final system can execute.

For the compilation of the program, we generally think of it as "compile" and "link" two parts is enough, here the compilation has included preprocessing, compiled into assembly language and compiled into a target file three steps. As long as the header file is complete, the syntax is correct, compile generally can pass. Links can also be successful as long as there is a complete target file and a library file. As long as the compilation passed, the link is also passed, the entire project is compiled even if completed.

The whole process of compiling C programs under Linux

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.