Compiler g ++

Source: Internet
Author: User

G ++ is a component in the GNU compilation Toolkit (GCC). It is used to compile source files written in C ++.

From the C ++ source file to the executable file compilation process, there are several steps below. g ++ provides many compilation options, allowing us to control the entire compilation process:

  • Pre-compiled (G ++ option-e) results are directly output to the console
  • Compile (G ++ option-S) and save the result as a. s file.
  • The result of assembly (G ++ option-C) is saved as a. o file and the target file is)
  • After the Link (G ++ option does not exist), the result is the executable file.

With these options, we can stop compilation after a certain step and output the result of that step.

Compilation can refer to the entire process from the source file to the executable file, or the second step above.

Pre-compile

In this step, the included header file is expanded and the defined macro is replaced. For example:

#define ONE 1#define TWO 2int add_one_two(){  return ONE + TWO;}

Save as add. cpp and compile it with the command g ++-e Add. cpp. The output result is as follows:

# 1 "add.cpp"# 1 "<built-in>"# 1 "<command-line>"# 1 "add.cpp"int add_one_two(){  return 1 + 2;}

Maybe you will ask, why not # include or main function?

No # include is because this example is very simple and does not use declarations or definitions in other files. The reason for not using the main function is that it is necessary to link to that step, just like we need a pot to make a scrambled eggs, but we only need a bowl to beat the eggs. Typing eggs is equivalent to pre-Compiling and does not require a pot, while frying eggs is equivalent to a link and requires a pot. The output is an executable file-fried eggs.

How are you doing.

Of course, you can also have these two things. Let's give you an exercise and use this step to experiment with the hello World Program in the previous blog. The result is too long for me to post it, but you must give it a try and it will surely surprise you.

The previous items are good for # include <iostream>. The pre-compilation will expand all the # include files and their own content in iosteam, this is also a method for viewing source files. If you look at it carefully, you will find that cout is an instance of the template class ). If you don't understand it, it doesn't matter. Let's take a look at it later.

Compile

This step translates pre-compiled files into assembly languages to generate a. s file. Yes, the pre-compiled files are still viewed by people, and the Assembly Language is already half Orcs; the entire compilation process is indeed a process of translation-from high-level languages to machine languages. Inventing a new language is an opposite process, but it can start with assembly.

The following is a simple function to calculate the sum of two integers:

int add(int a, int b){  return a + b;}

Run the command g ++-s add. cpp to generate Add. S. The content of ADD. S is as follows:

.file"add.cpp".text.globl_Z3addii.type_Z3addii, @function_Z3addii:.LFB0:.cfi_startprocpushq%rbp.cfi_def_cfa_offset 16.cfi_offset 6, -16movq%rsp, %rbp.cfi_def_cfa_register 6movl%edi, -4(%rbp)movl%esi, -8(%rbp)movl-8(%rbp), %eaxmovl-4(%rbp), %edxaddl%edx, %eaxpopq%rbp.cfi_def_cfa 7, 8ret.cfi_endproc.LFE0:.size_Z3addii, .-_Z3addii.ident"GCC: (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1".section.note.GNU-stack,"",@progbits

At the beginning, it was a little too big to come into contact with the Assembly, but it would be nice to see more. If you want to become a programming expert, you must be able to understand the assembly language. Click the example to enter (Example 1, Example 2). You do not have to be able to write, but you must be able to understand it.

The advantages of learning Assembly are: better understanding of pointers and references in C ++, understanding of function calls, parameter transmission, understanding of stacks, stack frames, and so on. More details will be discussed later. Anyone who can't wait to learn about compilation can read this blog. It's really not hard (click it ). This series of blogs is also good, but the topic is not a compilation (click to enter ).

Assembly

This step also translates the. s file in the assembly language in the previous step into a. o file in the machine language. This file is a binary file and is no longer visible to anyone. You can use a tool like objdump for disassembly.

Generated. the o file is called the target file. The English language is the object file. Each source file (note that it is not a header file) will generate a corresponding target file during the compilation process, in this way, the Disassembly and debugging of a file will become simpler, because the target file will usually become very large after the last step (Link), and it will be more difficult to debug.

For more details, refer to my previous blog:

Link

This step is to integrate all the target files generated in the previous step and link the required library files. For example, if you want to cook a fried rice with eggs and steamed rice, the link is to mix the two materials and stir them up, and then add some spices (other library files ). For details, refer to my blog.

Summary

Of course, the compilation options provided by G ++ are far more than those above. The summary of the G ++ help file (man) is as follows:

GCC [-c |-S |-E] [-STD = standard]
[-G] [-PG] [-olevel]
[-Wwarn...] [-pedantic]
[-Idir...] [-ldir...]
[-Dmacro [= defn]...] [-umacro]
[-Foption...] [-mmachine-option...]
[-O outfile] [@ file] infile...

Only the most useful options are listed here; see below for
Remainder. g ++ accepts mostly the same options as GCC.

This blog introduces only the options in the first brackets. There are various options in the brackets that follow. debugging options, warning options, and code optimization options are commonly used, -O option. Therefore, G ++ is very powerful and easy to use. To use it well, you need to practice more.

The options in brackets indicate that these options are optional and only the infile is required. If there are no options, such as G ++ hello in my previous blog. CPP and G ++ compile the executable files according to the default options. I don't know what the default options are. I am wrong...

(The red part) is: only useful options are listed, and the rest are listed (I only paste a small part of the Help file, there are still a lot of unpasted pages. Let's take a look at this command: man G ++, Pageup, Pagedown ). G ++ accepts similar options as GCC. (GCC is the C language compiler of GNU)

During the compilation process, G ++ reports various errors to us due to various problems. It mainly focuses on compiling and linking.

Syntax errors and declarations during program writing will be given at the compilation step. The definition issue will be given at the link. The declaration and definition are two problems. You need to pay attention to them. I will go into depth later.

Now, let's talk about G ++.

From the time I wrote my blog "several tips for learning Linux programming", I finally found myself out of the question, and I knew I needed to write a series, the topic of the series is the first sentence of the blog: "I have used Linux for 3 or 4 years, and I have accumulated some experience from cainiao to cainiao, now I will share with you, especially for beginners. "

Here, beginners are mainly college students in 1234, including students with no programming experience or Windows programming experience. In my opinion, Windows programming is a comfortable cage, and Linux programming is a world full of wind and rain. A comfortable cage is also a cage. One day, you will always feel bound. In the stormy world, you can build a castle that is times more comfortable than a cage. Through this series, I hope that I can help beginners build their own cabins in the Linux world. As for creating a castle, you can see your imagination and creativity.

Finally, I hope you can practice more. If you do not know what exercises to use, please refer to my previous blog :)

Good luck.

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.