Use of GCC

Source: Internet
Author: User

GCC is a common C/C ++ compiling driver in Linux. This article first introduces the entire GCC compilation connection process, then discusses common GCC options, and finally illustrates the relationship between GCC and G ++.

GCC compilation connection process

The GCC programming connection process is as follows:


CPP here is a C Preprocessing Program (for example, you can directly call CPP hello. c-O hello. I output the preprocessing result to hello. i); CC is the C compiler, which sets hello. I compiled into an ASCII assembly language file hello. s; as is the assembly compiler, which sets hello. S is translated into a retriable destination file hello. o; LD is a connection program. It combines all the deduplication target files to generate an executable file. In other words, GCC is a compilation driver that calls the above program (such as CPP and CC) to generate executable files. You can use the GCC option to generate these intermediate files as follows:

After gcc-e hello. C-o preprocess_hello. I executes this command, preprocess_hello. I stores the pre-processed C program.
After gcc-s hello. C-o assemble_hello.s executes this command, assemble_hello.s stores the assembly code of the program. It is equivalent to directly executing the preprocessing and compilation steps.
After gcc-C hello. C-o object_hello.o executes this command, object_hello.o saves the machine code of the program and cannot be executed because there is no connection. If you want to perform the preceding three steps at the same time.

We can use option-save-temps to keep all intermediate files when generating executable files. For example, GCC-save-temps-O hello. C can retain the intermediate file, that is, the file hello. I, hello. O, hello. s and the executable file Hello are generated at one time.

Common GCC options

GCC can use the GCC -- Help Command to view options. The usage of GCC options can be summarized as follows:


Common options are summarized as follows:


The following describes the common options:

-X is used for the language of the specified program. If the option is changed, GCC no longer uses the file suffix to determine the language type.
The-V option can output the output information of the entire GCC compilation connection process, including the query path and other information. For example, GCC-V hello. C (of course, you can use gcc-V hello. c> & temp output file), in addition to generating. in addition to the out executable file, a large amount of additional information will be output, such as # include "... "And # include <...> the value of LIBRARY_PATH (The Connection Program searches for special connection files in these directories.
The-D option can be used to define macros. For example, you can define the macro in the command line: gcc-dmacro hello. C is equivalent to setting the macro at the beginning of the file, that is, # define MACO, but the definition in the command line is more flexible. For example, these statements are available in the source code.

  #ifdef DEBUG   printf("this code is for debugging\n");  #endif

If the-ddebug option is added during compilation, the compiler will compile the row where printf is located into the target code, so as to easily track the status of some programs in this position. In this way, ddebug can be used as a debugging switch. When compiling, you can add it to print debugging information. When publishing, you can remove this compilation option to remove debugging information. You can also define a value, such as gcc-ddebug = 42 hello. c, so that this value is used in the Code, such as printf ("this code is for debugging, % d \ n", debug); the predefined value is 1 by default.

-Wall opens a useful warning option. This option is used to detect compilation.
The default GCC option-O is not optimized. With-O2,-OS can optimize the code execution speed and size based on different running platforms.
-STD = c99 indicates that the program is compiled using the standard ISO c99. For example, GCC-STD = c99 hello. C.
-Xlinker can be used for static library link sequence problems. If there are multiple paths in the GCC option, they are all in the search order of the specified path.

Relationship between GCC and G ++

Both GCC and G ++ are GNU compilation drivers, but there are some differences between them:
Suffix. c file GCC treats it as a C program, G ++ as a C ++ program, both the suffixes are c ++ as C ++ programs (that is, GCC is also equivalent to the predefined macro _ cplusplus ). For CPP programs, both the GCC and G ++ compilation phases are the same, and GCC is used for compilation. However, in the Link stage, GCC cannot automatically connect to C ++ programs using libraries, g ++ automatically connects to these databases. For example, compile with G ++: G ++ hello. CPP-O cpphello, while compiling with GCC requires connecting to the common standard library of C ++: GCC hello. CPP-lstdc ++-O cpphello. Note that the generated executable files are not exactly the same.

References

Http://wangcong.org/articles/learning-gcc.cn.html
Http://www.yolinux.com/cgi-bin/man2html? Cgi_command = gcc

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.