Some common usage of GCC in Linux

Source: Internet
Author: User

Some common usage of GCC in Linux


1. The GCC compiler compiles programs in four stages:

1> pre-compilation (pre-processing): In the pre-processing stage, the input is a C source file, usually *. C. They generally contain header files such as *. h. This phase mainly processes the # ifdef, # include, # define preprocessing commands in the source file. In this phase, an intermediate file *. I is generated.

In this stage, run the following command:

Gcc-e tiger. C-o tiger. I

It uses the-E Option for the source file tiger. C to generate the intermediate file tiger. I

2> compile and optimize: In the compilation phase, the intermediate file * I is input, and the compilation file *. S is generated after compilation.

In this stage, run the following command:

Gcc-s tiger. I-o tiger. s

3> assembing: During the Assembly stage, the input Assembly file *. S is converted to the binary machine code *. O.

In this stage, run the following command:

Gcc-C tiger. S-O tiger. o

4> Link (linking): At last, the binary machine code file *. O is input in the Link phase, which is combined into an executable binary code file.

In this stage, run the following command:

GCC tiger. O-o Tiger

Note:

In all the above stages, you can directly use gcc-O Tiger tiger. C to generate executable files.

Gcc-O Tiger tiger. C (you can also use GCC tiger. C-o Tiger)

2. In actual development, when you use GCC to compile source code to compile a program, there are usually more than one source file. In this case, you need to use GCC to compile multiple source files.

Run the following command:

Gcc-otiger tiger1.c tiger2.c tiger3.c

This command simultaneously compiles three source files tiger1.c, tiger2.c, tiger3.c, and finally generates an executable program Tiger (the command does not contain *. H files)

Note: whether a program has only one or more source files, it must have only one and only one main function in all compiled and linked source files, because main () is the population of every program.

3. source files, target files, and executable files are common terms in the compilation process.

1> A source file is usually a file that stores editable Code. For example, a C file suffix is. C, and a C ++ file suffix is. cpp;

2> the target file refers to the binary code that can be recognized by the CPU generated by compilation by the compiler. However, the target file cannot be executed because some of the function processes have no relevant instructions or instructions.

3> the executable file is the file linked to the target file and the relevant library. It can be executed.

4> during the pre-compilation process, the header files referenced in the program are included in the source code and some macros are replaced.

5> the compilation process translates user identifiable languages into a group of operator codes that can be recognized by the processor to generate the target files, which are usually translated into an assembly language, however, there is a one-to-one relationship between the Assembly Language and the machine operation code.

6> All target files must be combined in some way to run. This is the role of the link. The target file only parses the variables and functions in the file. The referenced functions and variables are not parsed yet, this requires referencing other compiled target files to parse unparsed variables and functions. Generally, the referenced target file is the library. After the link is complete, an executable file is generated.

4. Basic GCC usage

GCC [Options] [filenames]

Options is required by the compiler, and filenames is the relevant file name.

1>-C: only compiled, not linked to executable files, the compiler is input. c. O is a target file with a suffix. It is usually used to compile a subroutine file that does not contain the main program.

2>-ooutput_filename: Determine the output file name output_filename. If this option is not used, GCC defaults to the source file name. o file.

3>-G: generate the symbolic information required by the Debugger GDB. To debug the source code, you must add this option when compiling the program.

4>-wall: Used to enable compilation warnings, which helps programs capture errors.

5>-O: optimizes program compilation and links. With this option, the entire source code will be compiled and processed during the link process, the execution efficiency of such executable files is high, but the speed of compiling links is relatively slow.

6>-O2: it is better to optimize compilation and linking than-o, but the whole compilation process will be slower.

Note:

Use G ++ to compile the C ++ program. The C ++ source program suffix is. cpp.

Use G ++ to compile the C ++ program. The subsequent parameter usage is the same as that in GCC;

Eg:

G ++-otiger tiger. cpp

After the execution, the execution program with the execution file name tiger is generated.

We can also use GCC to compile the C ++ program. when compiling the program, we need to tell the system to use the C ++ standard library instead of the C ++ standard library; therefore, the C ++ library must be specified during compilation.

Eg:

Gcc-O Tiger tiger. cpp-lstdc ++

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.