gcc/g++ the compilation process of actual combat

Source: Internet
Author: User

gcc and g++ are c&c++ compilers for GNU (an open source organization), respectively

For the. c suffix file, gcc regards it as a C program, g++ as a C + + program, and GCC and g++ as C + + programs for. cpp suffix files. GCC can be compiled by C programs and C + + programs according to the suffix named. C or. cpp, but g++ is compiled by C + + programs uniformly, either. C or. cpp.

During the compile phase, g++ calls GCC, and the link phase is usually done with g++, g++ in the process of compiling, actually calling GCC to compile according to C + + programs. That is, the compilation work is ultimately done by GCC. This is because GCC commands are not automatically connected to libraries used by C + + programs. G++ will automatically invoke the linked C + + library.

GCC can be used to compile C + + but it does not automatically invoke the linked C + + library, you need to manually link it, using the following command:

GCC main.cpp-lstdc++

Note: main.cpp-lstdc++ position can not be changed, changed position the author appeared main.cpp: (. text+0x2d): Undefined reference to ' Std::cout ' and a series of errors


gcc/g++ is divided into the following four procedures when compiling a job:

1. Preprocessing, generating. I files
2. Convert the preprocessed file into assembly language, generate the. s file
3. Assembly into Target code (machine code) generate. o Files
4. Connect the target code to generate an executable program

Here is A small example of these four processes://Build a main.cpp
// This is the test code #include <iostream>
using namespace std;  
#define Pi 3.14
Static int 1
int Main () { cout<<""<<t+pi<<Endl;    return 0 ;

  (1) pretreatment stage

After preprocessing the file Linux with. I as the suffix, this process only activates preprocessing, does not generate files, so you need to redirect it to an output file.

 The function of this step:

The substitution of macros, as well as the elimination of annotations, and the associated library files are found. Open main.i with the editor will find a lot of code, you just need to see the last part of the change, the preprocessing has done a macro substitution, as well as the elimination of annotations, can be understood as irrelevant code cleanup.

Cat Main.i

The following is the last part of the main.i file, where you can see the substitution of macros and the elimination of annotations.

(2) Convert the preprocessed file into assembly language, generate. s file
g++-S Main.cpp
The function of this step:generate the Main.s file, the. s file means the assembly file, and the editor opens it all as assembly instructions.
Cat MAIN.S

The following is part of the Main.s file:

(3) the assembly becomes the target Code (machine code) to generate. o Files

The function of this step:. O is a gcc-generated target file that is opened with an editor and is binary machine code.  

Cat MAIN.O

The following is part of the MAIN.O file:

(4) Connect the target code to generate the executable program
g++ Main.o-o main//generated executable program named Main, if the execution of command g++ MAIN.O  such a default generation A.out, that is, main and A.out is a just a different name

The following is part of the main file:
Ls

./main

 

After a successful compilation, the link stage is entered. Here comes an important concept: the Library of functions (which can be understood so that the. cpp without the main () function is generated).

The reader can re-view this applet, in this program does not define "cout" function (exactly said cout is not a function, cout is very unique: it is not a function, it does not seem to be specifically defined by C + + like a special syntax such as if,for "statement", in fact, the letter Number of calls , but this function is somewhat special, with operator overloading, or, rather, overloading the ' << ' operator. If you use the PRITF () function to explain the better, for the moment as a function to understand it) implementation, and in the pre-compilation included in the "iostream" is only the declaration of the function, but not the implementation of the definition function, then, where is the implementation of the "cout" function? These functions are implemented by the system is named Stdc++ Library file, in the absence of special designation, g++ will go to the system default search path "/usr/lib" to find, that is, link to the stdc++ library function, so that the function "cout" can be implemented, And that's the role of the link.

function library is generally divided into static library and dynamic library two kinds.

The next article will be described in detail.

gcc/g++ the compilation process of actual combat

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.