LINUX compiler GCC usage

xiaoxiao2021-03-06  106

Used of embedded Linux compiler GCC

1, GCC overview

As a flagship project of free software, Richard Stallman just started writing GCC more than a decade, but only as a compiler of a C program language, GCC meaning is just GNU C Compiler.

After so many years of development, GCC can not only support C language, it also supports Ada Language, C Language, Java Language, Objective C Language, Pascal Language, COBOL, and supports functional programming and logical programming Mercury language. Wait. The GCC is no longer a single GNU C language compiler, but has become a GNU compiler family.

The GCC's compile process is divided into 4 steps, respectively:

l pre-processing

l Compilation (Compiling)

l Assembling

l Link (Linking)

The compiler can distinguish the language used by the program's extension, which is different from the steps that the compilation needs to be executed due to different programs, so the GCC processes them separately according to different suffixes, and the table indicates different suffixes. Name processing method:

GCC supports the seducement name explanation

The language compilation process corresponding to the suffix name. CC original program pre-processing, compile, assembly .c / .cc / .cxxc original program pre-processing, compile, assembly .MOBJECTIVE-C original program pre-processing, compilation, assembly .i already Handling C original program compiled, assembly .ii has been compiled, compiling. S / .S assembly language original program compilation file (header file) .o Target file link. A / .SO compiled library file link

2, GCC compilation process analysis

The basic syntax used by GCC is:

GCC [option | filename]

l Option is some of the options when GCC usage, and power can be achieved by specifying different option GCCs.

l The filename here is a file to be compiled. The GCC will process the compilation file according to the compilation option specified by the user and the identified file suffix name.

Here, the common use of GCC is explained from the perspective of the compile process. First, there is a simple C language program that consists of two files, where "Hello.h" is the header file, "Hello.h" is included in "Hello.c", and its source file is as follows .

/ * Hello.h * /

#ifndef_hello_h_

#define_hello_h_

TYPEDEF UNSIGNED Long VAL32_T;

#ENDIF

/ * Hello.c * /

#include

#include

#include "hello.h"

Int main (int Argc, char * argv [])

{

VAL32_T I = 5;

Printf ("Hello, Embedded World% D \ N", i);

}

A. Pretreatment phase

GCC's option "-e" can cause the compiler to stop compilation at the end of the pre-processing, the option "-o" is the result of the specified GCC output, and its command format is as follows.

GCC -E -O [target file] [Compilation file]

We already know that files named ".i" are pre-processed C original programs. It is to be noted that the "Hello.h" file cannot be compiled, so the command that causes the compiler to stop after pre-processing as shown below.

[root @ localhost gcc] # gcc -e -o hello.i hello.c here, option '-o' refers to the target file, and '.i' file is the C original program that has been pre-processed. The part of the Hello.i file is listed below.

# 2 "Hello.c" 2

# 1 "Hello.h" 1

TYPEDEF UNSIGNED Long VAL32_T;

# 3 "Hello.c" 2

int main ()

{

VAL32_T i = 5;

Printf ("Hello, Embedded World% D \ N", i);

}

It can be seen that GCC does preprocess, it inserts "Hello.h" in the Hello.i file.

B. Compilation phase

After the compiler ends, the compiler enters the compilation phase, and the GCC first checks the code specifically, whether there is a syntax error, etc., to determine the actual work of the code, after the check is correct, start the code Translated into assembly language, GCC option "-s" can cause the compiler to stop before making it. We already know, ". S" is the original program of the assembly language, so the target file here can be set to ".s" type.

[root @ localhost GCC] # gcc -s -o hello.s Hello.i

The content of Hello.s is listed below, and it can be seen that GCC has turned into compilation. If you are interested, you can analyze how this line is simple C-language applet use assembly code how it is implemented.

.file "hello.c"

.SEction.RODATA

.Lc0:

.string "Hello, Embedded World% D \ N"

.Text

.globl main

.Type main, @ function

MAIN:

Pushl% EBP

MOVL% ESP,% EBP

SUBL $ 8,% ESP

Andl $ -16,% ESP

MOVL $ 0,% EAX

Add1 $ 15,% EAX

Add1 $ 15,% EAX

SHRL $ 4,% EAX

SALL $ 4,% EAX

SUBL% EAX,% ESP

MOVL $ 5, -4 (% EBP)

SUBL $ 8,% ESP

PUSHL-4 (% EBP)

Pushl $ .lc0

Call Printf

Add1 $ 16,% ESP

Leave

RET

.size main, .- main

.SECTION.NOTE.GNU-stack, "" @ progbits

..IDENT "GCC: (GNU) 4.0.0 20050519 (Red Hat 4.0.0-8)"

We can see that the program of this small C language has been complicated in assembly, which is also the advantage of the C language as the so-called intermediate language.

C. Compilation phase

The assembly phase is the ".s" file generated by the compilation phase generates the target file. The reader uses the option "-c" here to see the assembly code has been converted to ".o" binary target code. As follows.

[root @ localhost gcc] # gcc -c hello.s -o hello.o

D. Link Stage

After successful compilation, you have entered the link phase. Here, there is an important concept: a library.

Question: The function of "Printf" is not defined in this program, and only the "stdio.h" in the precompilation is only the declaration of the function, and there is no implementation of the function, then where is it realized? " PRINTF "function?

answer:

The system has been put into the library file named libc.s 6, and the GCC will find the system's default search path "/ usr / lib" when there is no special specification. That is Link to the libc.so.6 library function, so that the function "Printf" can be implemented, this is the role of the link. After completing the link, the GCC can generate an executable, and the command is as follows:

[root @ localhost gcc] # GCC Hello.o -o Hello

Run the executable, the correct result is true.

[root @ localhost gcc] # ./hello

Hello, Embedded World 5

转载请注明原文地址:https://www.9cbs.com/read-96226.html

New Post(0)