C language program compilation process

Source: Internet
Author: User
Tags comparison table

Recently, when compiling an ARM program of DM8168, some errors such as undefined and redefinition are often reported. Due to the large number of source code files and many inclusion relationships, it is easy to mess up when you add your own programs. I deeply understand that a good code style is so important that I have been looking at code refactoring before. I should pay more attention to the quality of code in the future. The summarized rules are as follows:

1. Write a common data structure into a head file. Other source files contain this header file. At the same time, to allow functions in different source files to be used, common functions can be declared in this header file.

2. variables declared in other source files. If you want to use them in another file, the extern declaration is required to avoid the interaction of various global variables.

I hope you can give me some advice.

The gcc compilation process is attached below:

The following is a brief summary of how to compile hellworld with GCC.

The source code of hello. c is as follows:

# Include
Int main ()
{
Printf ("Hello, world. \ n ");
Return 0;
}

We usually use gcc to generate an executable program. The command is gcc hello. c. The Executable File a. out is generated by default.

In fact, the command for compiling (including links): gcc hello. c can be divided into the following four major steps:

· Preprocessing)

· Compile)

· Assembly)

· Link (Linking)

1. Preprocessing)

The Preprocessing process mainly includes the following processes:

Delete all # define statements and expand all macro definitions.
Process all conditional pre-compiled commands, such as # if # ifdef # elif # else # endif.
Processing # include pre-compilation instructions: insert included files to the pre-compilation instructions.
Delete all comments "//" and "/**/".
Add a line number and a file ID to generate a line number for debugging and a warning line number for compilation errors during compilation.
Keep all # pragma compiler instructions because the compiler needs to use them


The following commands are usually used for preprocessing:

Gcc-E hello. c-o hello. I

The-E parameter indicates that only preprocessing is performed or the following command can be used to complete the preprocessing process.

Cpp hello. c> hello. I/* cpp-The C Preprocessor */

Directly cat hello. I, you can see the pre-processed code.

2. Compile (Compilation)

The compilation process is to perform a series of lexical analysis, syntax analysis, Semantic Analysis and Optimization on the pre-processed files to generate the corresponding assembly code.

$ Gcc-S hello. I-o hello. s

Or

$/Usr/lib/gcc/i486-linux-gnu/4.4/PC3 hello. c

Note: In the current version of GCC, the preprocessing and compilation steps are combined into one step, which is completed by using the tool "cc1. Gcc is actually some packaging of background programs, called according to different parametersOthersThe actual processing program, such as: Pre-compiled program, assembler as, connector ld

The compiled assembly code (hello. s) is as follows:

. File "hello. c"

. Section. rodata

. LC0:

. String "Hello, world ."

. Text

. Globl main

. Type main, @ function

Main:

Pushl % ebp

Movl % esp, % ebp

Andl $-16, % esp

Subl $16, % esp

Movl $. LC0, (% esp)

Call puts

Movl $0, % eax

Leave

Ret

. Size main,.-main

. Ident "GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3"

. Section. note. GNU-stack, "", @ progbits

3. Assembly)

Assembler converts assembly code into commands that can be executed by machines. Each assembly statement corresponds to almost one machine command. Compared with the compilation process, the compilation process is relatively simple. You can translate the compilation commands one by one based on the comparison table of the Assembly commands and machine commands.

$ Gcc-c hello. c-o hello. o

Or

$ As hello. s-o hello. co

Because the content of hello. o is a machine code, it cannot be viewed in the form of common text (if you open the vi, you will see garbled code ).

Zookeeper

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.