Introduction to GCC Compilation

Source: Internet
Author: User

GCC (gnu c compile) is a standard C compiler in Linux. It can compile C, C ++, Object C, and other languages. In addition, GCC supports compiling on the exaggerated platform, that is, the current CPU platform develops software for different hardware platforms and architectures. (GCC 4.8.2 and ubuntu14.02 are used in the following articles)

 

(1) install gcc

sudo apt-get install gcc

 

View the GCC version command after installation: gcc-V

 

(2) program compilation principles

Command Format of GCC compiler:

gcc [options] [filenames]

 

To better understand GCC commands, it is necessary to understand program compilation principles. For the GCC compiler, program compilation must go through the following four phases:

Pre-processing)

Compile (Compiling)

Assemling)

Connection (linking)

The following describes the four phases in detail:

  • Pre-processing)

The pre-processing phase is completed by the GCC pre-processor (CPP). Its function is to evaluate the macro, compile the conditions, and other work that requires passing the code to the compiler, the statements following # are usually processed by CPP.

Run:

gcc -o hello.i -E hello.c


Part of the hello. I file is as follows:

# 1 "hello.c"# 1 "<command-line>"# 1 "/usr/include/stdc-predef.h" 1 3 4# 1 "<command-line>" 2# 1 "hello.c"# 1 "/usr/include/stdio.h" 1 3 4# 27 "/usr/include/stdio.h" 3 4# 1 "/usr/include/features.h" 1 3 4# 374 "/usr/include/features.h" 3 4# 1 "/usr/include/x86_64-linux-gnu/sys/cdefs.h" 1 3 4# 385 "/usr/include/x86_64-linux-gnu/sys/cdefs.h" 3 4# 1 "/usr/include/x86_64-linux-gnu/bits/wordsize.h" 1 3 4# 386 "/usr/include/x86_64-linux-gnu/sys/cdefs.h" 2 3 4# 375 "/usr/include/features.h" 2 3 4# 398 "/usr/include/features.h" 3 4# 1 "/usr/include/x86_64-linux-gnu/gnu/stubs.h" 1 3 4# 10 "/usr/include/x86_64-linux-gnu/gnu/stubs.h" 3 4# 1 "/usr/include/x86_64-linux-gnu/gnu/stubs-64.h" 1 3 4# 11 "/usr/include/x86_64-linux-gnu/gnu/stubs.h" 2 3 4# 399 "/usr/include/features.h" 2 3 4# 28 "/usr/include/stdio.h" 2 3 4....# 2 "hello.c" 2int main(){printf("Hello! This is our embedded world!n");return 0;}


It can be seen that CPP inserts the content in the stdio. h file into the hello. I file, that is, preprocessing.

Compile

In the compilation phase, GCC will perform the following tasks: Check the code standardization and syntax errors, determine the actual tasks to complete the code, and translate the C language into an assembly language after confirmation, the compilation language is not converted into binary code. In this stage, if a program encounters an error, this error is called a compilation error.

Run:

gcc -o hello.s -S hello.i

Some content of Hello. A is as follows:

.file   "hello.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        popq    %rbp        .cfi_def_cfa 7, 8        ret        .cfi_endproc.LFE0:        .size   main, .-main

Assembly)

In the compilation phase, the compiled ". s" file is converted into the target binary code.

Run

gcc -o hello.o -c hello.s

Connection

After successful compilation, it enters the link stage. This involves an important concept: function libraries.

Readers can re-view this applet. In this program, the function implementation of "printf" is not defined, and the "stdio. h "is only the declaration of the function, but does not define the implementation of the function. So where does it implement the" printf "function? The final answer is: The system implements these functions named libc. so.6 is included in the library file. If it is not specified, GCC will search for it under the default search path "/usr/lib", that is, link to libc. go to the so.6 library function to implement the function "printf", which is the role of the link.

Function libraries are generally divided into static libraries and dynamic libraries. A static library is used to add all the code of the library file to the executable file when compiling the link. Therefore, the generated file is large, but the library file is no longer needed at runtime. The suffix is generally ". ". On the contrary, the dynamic library does not add the code of the library file to the executable file during the compilation link, but loads the library from the link file during the program execution, this can save the system overhead. The dynamic library is generally suffixed with ". So". As mentioned above, libc. so.6 is the dynamic library. By default, dynamic libraries are used for GCC compilation.

 

Introduction to GCC Compilation

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.