Introduction to GCC/g ++ basic commands

Source: Internet
Author: User

Original article address

GCC & G ++ is now the main and most popular C & C ++ compiler in GNU.
G ++ is a C ++ command, mainly used as. cpp. The C language suffix is generally. C. At this time, the command can be changed to GCC. In fact, it is irrelevant.
In fact, the compiler uses GCC or G ++ to determine whether the link is compiled according to the C standard or the C ++ standard.


The following uses test. cpp as an example:

Command: G ++ test. cpp
Function: generate a file named a.exe, which contains compilation and links.
In addition, the-O command, the-O command indicates the output meaning. The GCC/g‑runtime command is very flexible. When you do not specify the output file name, the file is generated. EXE.
To export test.exe, you can use: G ++-O test.exe test. cpp. -The ocommand is output, and test.exe is output.

Gcc/g ++ takes the following steps to compile a program:





1. pre-process and generate the. I file [pre-processor CPP]

Command: G ++-E test. cpp> test. I

Function: output the pre-processed file. in Linux, use. I as the suffix. Only activate preprocessing. This does not generate a file. You need to redirect it to an output file. This step mainly involves the replacement of macros, the elimination of annotations, and the identification of related library files. Open test in the editor. I will find that there are a lot of code, you just need to look at the last part, you will find that the preprocessing is replaced with macros, there is also the elimination of comments, can be understood as irrelevant code clearing. The following is the last part of the test. I file. You can see macro replacement and comment elimination.


 

 

2. Do not convert the pre-processed files into assembly languages to generate the file. s [compiler egcs]

Command: G ++-s test. cpp
Function: generate the test. s file. The. s file indicates an assembly file. open the file in the editor as an assembly command.

3. a. o file is generated by means of compilation into the target code (machine code) [assembler as]

Command: G ++-C test. cpp
Function:. O is the target file generated by GCC. It makes no sense to open this. o unless you debug and develop the compiler and connector. Binary machine codes cannot be read by ordinary people.

4. Connect the target code to generate an executable program [linker ld]

Command: G ++ test. O-l f: \ vs2008 \ Vc \ include \ iostream
Function: link the. ofile and the required database file to form an executable file. -L indicates the link. Here I will write an absolute path, which is different from each other's computer.


In the above steps, you can use the-O command to output the names you want. For example, use the following output test.exe
You can use g ++ test. O-o test.exe-l f: \ vs2008 \ Vc \ include \ iostream


After writing the above code, I hope to understand how the entire source file is converted into an executable file. GCC/g ++ is really awesome *)

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.