Linux C program compilation and linux Compilation

Source: Internet
Author: User

Linux C program compilation and linux Compilation

Compilation process of Linux C program

 

 

I think I still have to learn the compiling rules of a language program. Now, I will summarize my understanding of C compilation through a small example.

/* Test. c understand C program compilation */

# Include <stdio. h>
Int main (void)
{
Printf ("Hello World! \ N ");
Return 0;
}

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

Gcc-o test. c or gcc test. c-o test

In fact, the preceding Compilation command contains four stages of processing: Preprocessing (also called Preprocessing), Compilation, Assembly, and Linking ).

 

The complete compilation process is detailed here.

Preprocessing:

Purpose: The main function of preprocessing is to read the source code, check the statements and macro definitions containing the pre-processing commands, and convert the response of the source code. The pre-processing process also deletes comments and extra spaces in the program.

Object: preprocessing commands start with "#". Preprocessing objects mainly include the following:

(1) # define macro definition

(2) # operator # The operator is used to convert the parameter following it into a string.

/*** Example ***/
# Define PASTE (n) "adhfkj" # n
Int main ()
{
Printf ("% s \ n", PASTE (15 ));
Return 0;
}
/******** Output adhfj15 *********/

(3) ## operator ## the function of the operator is to connect parameters together.

/***** Example *****/
# 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;
}
/* The output of the program is aabbcc **********/
(4) Conditional compilation commands

(5) Instructions for header file inclusion

(6) special symbols

_ FILE _ string containing the current program FILE name

_ LINE _ indicates the integer of the current row number.

_ DATE _ string containing the current DATE

_ TIME _ contains the current string

The Preprocessing command for the test. c file above is

Gcc-E test. c-o test. I

Compile-compile into assembly language

Gcc-S test. I-o test. s

This is the content of test. s compiled by the above Code.

. File "test. c"
. Section. rodata
. LC0:
. String "hello world"
. Text
. Globl main
. Typemain, @ function
Main:
. 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
Callputs
Movl $0, % eax
Leave
. Cfi_def_cfa 7, 8
Ret
. Cfi_endproc
. LFE0:
. Sizemain,.-main
. Ident "GCC: (GNU) 4.4.7 20120313 (Red Hat 4.4.7-4 )"
. Section. note. GNU-stack, "", @ progbits

Assembly

Purpose: Compile the preceding assembly instructions to generate the target file.

Gcc-c test. s-o test. o

This is the content of the test. o file above.

ELF> 8 @@
UH huh? ? ? Hello world GCC: (GNU) 4.4.7 20120313 (Red Hat 4.4.7-4) zR x? A? CP. symtab. strtab. shstrtab. rela. text. data. bss. rodata. comment. note. GNU-stack. rela. eh_frame @? 0 & X, X 1X9 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 with the target file to be appended, and finally generate an executable file. The attached target file also includes the required library files (static and dynamic link libraries)

Gcc test. o-o test

The final generated test file is the file that can be executed by the final system.

 

For program compilation, we generally think that "Compilation" and "Link" are sufficient. The compilation here includes preprocessing, three steps are required: Compile the program into an assembly language and compile the program into a target file. As long as the header file is complete and the syntax is correct, compilation is generally successful. If you have a complete target file and function library file, the link is successful. As long as the compilation passes and the link passes, the compilation of the entire project is complete.

 

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.