Linux system gcc common commands and GCC compilation process description

Source: Internet
Author: User

Objective:

GCC was formerly the GNU C language compiler (GNU C Compiler) because it could only handle the C language. GCC expands quickly to become able to handle C + +. and then
Extensions can support more programming languages such as FORTRAN, Pascal, Objective-c, Java, Ada, go, and assembly language on a variety of processor architectures, so changing
GNU Compiler Suite (GNU Compiler Collection). (Baidu Encyclopedia, want to know their own query)


A The process of compiling the GCC program is about four stages.

    • Pretreatment (pre-processing)
    • Compiling (compiling)
    • Compilation (assembling)
    • Links (linking)

Two Simple HelloWorld Program compilation process

Example: HELLOWORLD.C

1 #include <stdio.h>2int  main ()3{4 printf ( " Hello world!\n " ); 5 return 0 ; 6 }   

Ctrl+alt+t---> Open console---> Input vim helloworld.c

1. Execute the following command: $ gcc-o HelloWorld HELLOWORLD.C


Run as follows: $./helloworld
Output: helloworld!


2. Step through the compilation as follows:
(1) $ gcc–e helloworld.c-o helloworld.i

You can output the code in the helloworld.i file that contains the HELLOWORLD.C after preprocessing. Open the helloworld.i file and take a look, and you'll see. The subsequent instruction is to output the preprocessed code directly in the command-line window.

The-e option of GCC allows the compiler to stop after preprocessing and output the preprocessing results. In this case, the preprocessing result is to insert the contents of the stdio.h file into the HELLOWORLD.C.


End of preprocessing
When you look at the HELLOWORLD.I, you can see a lot of things plugged in.


(2) $ gcc–s helloworld.i

The-s option of GCC, which indicates that the assembly code is stopped and the-O output assembly code file is generated during program compilation.

View next Helloworld.s//Generate assembly code after end



(3) After generating the assembly code file Helloworld.s, it is necessary for the gas assembler to compile it as the target file, as follows:

$ gcc–c HELLOWORLD.C
Or:
$ gcc-c Helloworld.c–o HELLOWORLD.O
Or:
$ gcc-c Helloworld.i-o HELLOWORLD.O

Compile End
Generate HELLOWORLD.O File


(4) $ gcc helloworld.o–o HelloWorld generate connection, executable file (Hellworld executable file rebuilt)

The GCC connector is provided by gas and is responsible for connecting the program's target file with all the additional target files needed to eventually generate the executable file.

Additional target files include a static connection library and a dynamic connection library.

For the HELLOWORLD.O generated in the previous section, connect it to the C standard input and output library, and the final build program HelloWorld the executable file

Input command: GCC helloworld.o–o HelloWorld---> Generated as:

3. Compilation of multiple Program Files

In general, a program is composed of several file source files, so the compilation needs to form a number of compilation units, using the GCC compiler can compile multiple source files into the required program

For example: source files have main.c a.c B.C eventually generate an executable file main

For example, a project has an executable file that main.c a.c B.C generate test.
The compile command is as follows:
$ gcc–c main.c A.C B.c–o Main
Or:
$ gcc–o main MAIN.C a.c B.C

GCC will still follow the process of preprocessing, compiling, and linking

The approximate process is equivalent to:

Gcc-c Main.c-o MAIN.O

Gcc-c A.c-o A.O

Gcc-c B.c-o B.O

At last:

GCC main.o a.o b.o-o Main

Linux system gcc common commands and GCC compilation process description

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.