GCC Compile detailed

Source: Internet
Author: User

Common Options

-E: Preprocessing only, not compiling
-S: Compile only, not assembler
-C: Compile, assemble only, not link
-G: Contains debug information
-I: Specify the search directory for include files
-O: Output to specified file name advanced options

-V: Verbose output Each of the options used during compilation
-C: Preserve annotation information while preprocessing
-GGDB: Include debug information in the executable file for GDB to use
-fverbose-asm: When compiling into assembly language, the name of c variable as the annotation in assembly language
-save-temps: Automatic output preprocessing files, assembly files, object files, compile normal
-fsyntax-only: Only Test source file syntax is correct, do not do any compile operation
-ffreestanding: Compiled into standalone programs, not host program language standards

-ansi:ansi Standard
-STD=C99:C99 Standard
-std=gnu89:iso/iec 9,899:1990 and GNU extensions
-std=gnu99:iso/iec 9,899:1999 and GNU extensions
-trigraphs: Support for ISO C three character group error tips

-W: Ignore All warnings
-werror: Does not distinguish between warnings and errors, and any warnings are stopped from compiling
-wall: Open Most warning tips
-wshadow: A statement block scope variable has the same name as another variable for a larger scope (this warning is not included in the-wall option and needs to be opened separately)
-wextra: Warn about all legitimate but questionable expressions Alert optimization Options

-o0: Turn off all tuning options
-O1: First-level optimization, use this option to make the executable file smaller, run faster, and not add too much compilation time, can be abbreviated to-O
-O2: Second level optimization, with almost all of the optimization technology, using this option will increase the compilation time
-O3: Third-level optimization, on the basis of-O2 to increase the production of inline functions, the use of registers and other optimization techniques
-os: This option is similar to-o2 to optimize the space occupied, but does not perform performance optimization, often used to build the final version





GNU CC (GCC) is an ANSI C-compliant compilation system in the GNU project that compiles programs written in languages such as C, C + +, and object C. GCC is not only powerful, but can also compile languages such as C, C + +, Object C, Java, Fortran, Pascal, Modula-3, and Ada, and GCC is a cross-platform compiler, It can develop software on the current CPU platform for a variety of different architectures hardware platform, so it is especially suitable for the development and compilation of embedded domain. The example in this chapter, unless otherwise noted, uses the GCC version of 4.0.0.

The basics of GCC entry

Table 3.6 gcc supported suffix name explanation

Suffix name

The corresponding language

Suffix name

The corresponding language

. C

C Original Program

. s/. S

Assembly Language Original Program

. C/.cc/.cxx

C + + Original program

. h

preprocessing files (header files)

. m

OBJECTIVE-C Original Program

. o

Destination file

. I

Already preprocessed C-original program

. a/.so

Post-compiled library files

. II

Pre-preprocessed C + + original program

As mentioned at the beginning of this chapter, the compilation process for GCC is divided into four steps, namely:

· Pretreatment (pre-processing)

· Compiling (compiling)

· Compilation (assembling)

· Links (linking)

Here's a concrete look at how GCC completes the four steps.

First, there are the following HELLO.C source code

#include <stdio.h>

int main ()

{

printf ("hello! This is our embedded world!n ");

return 0;

}

(1) pretreatment stage

At this stage, the compiler compiles the stdio.h in the above code, and the user can view it using the GCC option "-e", which is to let GCC stop the compilation process after the preprocessing ends.

Attention

The general format of the GCC directives is: gcc [options] The file to compile [options] [target file]

Where the target file can be default, GCC generates executable files by default, which is: Compile the file. Out

[Root@localhost gcc]# gcc-e hello.c-o hello.i

Here, the option "-O" refers to the target file, which is known by Table 3.6, and the ". I" file is a preprocessed C-source program. Some of the contents of the Hello.I file are listed below:

typedef int (*__GCONV_TRANS_FCT) (struct __gconv_step *,

struct __gconv_step_data *, void *,

__const unsigned char *,

__const unsigned char * *,

__const unsigned char *, unsigned char * *,

size_t *);

...

# 2 "hello.c" 2

int main ()

{

printf ("hello! This is our embedded world!n ");

return 0;

}

This shows that GCC did preprocess, inserting the contents of "stdio.h" into the hello.i file.

(2) Compile phase

The next step is the compile phase, in which GCC first checks the code for regularity, syntax errors, and so on to determine what the code actually wants to do, and when checked, GCC translates the code into assembly language. Users can use the "-S" option for viewing, which compiles without assembling and generates assembly code.

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

The following is a list of the contents of Hello.s, and it is clear that GCC has converted it into a compilation, and interested readers can analyze how this simple C-language applet is implemented in assembler code.

. File "Hello.c"

. section. Rodata

. Align 4

. LC0:

. String "hello! This is our embedded world! "

. text

. GLOBL Main

. type Main, @function

Main

PUSHL P

MOVL%esp, p

SUBL,%esp

Andl $-16,%esp

MOVL $, X

Addl $, X

Addl $, X

SHRL $, X

Sall $, X

Subl x,%esp

SUBL,%esp

PUSHL $. LC0

Call puts

Addl $16,%esp

MOVL $, X

Leave

Ret

. size main,.-main

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

. section. Note. Gnu-stack, "", @progbits

(3) Assembly phase

The assembly phase converts the compile-time ". S" file to the target file, where the reader can use option "-C" to see the binary target code that the assembly code has been converted to ". O". as follows:

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

(4) Link stage

After successful compilation, the link phase is entered. Here comes an important concept: the library of functions.

Readers can review this applet, in this program does not define "printf" the function implementation, and in the precompilation included in the "Stdio.h" also only the declaration of the function, and did not define the implementation of the function, then where the "printf" function is implemented. The final answer is: The system to achieve these functions are called libc.so.6 Library files, in the absence of special designation, GCC will go to the system's default search path "/usr/lib" under the lookup, that is, link to the Libc.so.6 library functions, so that the function can be implemented " printf ", and that's the role of the link.

function library is generally divided into static library and dynamic library two kinds. Static library refers to the compilation of the link, the library file to all the code into the executable file, so the resulting file is relatively large, but at run time will no longer need the library file. The prefix name is generally ". a". Dynamic libraries In contrast, when compiling a link, the code for the library file is not added to the executable file, but the library is loaded by the runtime linked file when the program is executed, which saves the overhead of the system. Dynamic Library general suffix named ". So", as described earlier libc.so.6 is a dynamic library. GCC uses dynamic libraries by default at compile time.

After the link is complete, GCC can generate the executable file, as shown below.

[Root@localhost gcc]# Gcc hello.o-o Hello

Run the executable file, and the correct results appear as follows.

[Root@localhost gcc]#./hello

Hello! This is our embedded world!

GCC compilation Options Analysis

GCC has more than 100 options available, mainly including overall options, alarms and error options, optimization options, and architecture-related options. The following is an explanation of the most commonly used options in each class.

(1) Overall options

The summary options for GCC are shown in table 3.7, many of which are already covered in the previous examples.

Table 3.7 gcc Total Options list

Suffix name

The corresponding language

-C

Just compile no link, generate Target file ". O"

-S

Just compile not assembler, generate assembly code

-E

Precompilation only, no other processing

-G

Include standard debugging information in an executable program

-O File

Output files to File

-V

Print command line information and compiler versions of the compiler's internal compilation process

-I. Dir

Add the dir directory to the list of search paths for header files

-L dir

Add the dir directory to the list of search paths in the library file

-static

Link Static Library

-llibrary

To connect a library file that is named Library

For the "-C", "E", "O", "-S" options, which are explained in the previous section, this is mainly about two other very common library dependency options "I-dir" and "L-dir".

· "-I dir"

As described in the table above, the "-I-dir" option adds the Dir directory to the list of search paths for header files. Because the Linux header file is placed in the "/usr/include/" directory by default, when the user wants to add a header file that is placed in another location, it can be specified with the "-I dir" option, so that GCC finds the corresponding directory at the appropriate location.

For example, under "/ROOT/WORKPLACE/GCC" There are two files:

#include <my.h>

int main ()

{

printf ("hello!! n ");

return 0;

}

#include <stdio.h>

This allows you to add the "-i" option to the GCC command line:

[Root@localhost GCC] GCC hello1.c–i/root/workplace/gcc/-O hello1

This will allow GCC to perform the correct results.

Little knowledge

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.