Linux-GCC Compiler Overview

About a .gcc

Because when developing applications under Linux, the basic use of the C language, so we are required to master at least one C compiler. The compiler can take advantage of our high-level language source code converted into binary code a computer can directly execute, because the CPU is not able to understand keywords main, int, double, etc. These C language, and executed by the CPU only recognizes 0, 1 consisting of both a digital binary code.
Linux is the most popular editor is gcc (GUN Compiler Collection), which is in line with the project GUN ANSI C compiler flags, to compile programs written in languages C, C ++, Java, Fortran , Pascal and Ada and so on. gcc is not only powerful, simple and flexible, but it also supports a variety of hardware platforms in embedded development on Linux also has a wide range of applications.

II. Compilation process procedures

When using gcc to compile the program, the compilation process can be divided into four steps:

  • Pretreatment
  • Compile
  • compilation
  • link

We test.ccompile the files as an example
following these four steps detailed description:

1. Pretreatment

In the pre-processing stage, gcc C file to be processed in the pre-command , that is, we are familiar with some instructions:

#define
#ifndef
#include

In this phase will generate intermediate files test.i, but in practice this does not generate special files, if you have to generate this file, you can use the following command gcc:

gcc -E test.c -o test.i

2. Compile

At compile time, the input stage of the pre-generated test.ifiles, generate an assembly language file compilertest.s , gcc command corresponding to this phase:

gcc -S -test.i -o test.s

3. Compilation

In the compilation stage, the input stage is to compile the generated test.sfiles after conversion machine generates a binary filetest.o , but this time the file is not binary machine executable computer files that can be executed directly , gcc command corresponding to this stage:

gcc -c test.s -o test.o

4. Links

In the link stage, the input stage is a binary machine code generated by the compilation test.o, linking this stage will be pooled with other file binary machine machine code files and libraries into an executable binary file , this step can use the following command gcc carry out:

gcc test.o -o test

This completes a full gcc compiler process, the last generation of testexecutable binary files can be directly executed by the computer.

5. Summary

This is more than four steps can be directly replaced with a single command:

gcc test.c -o test

When there are a plurality of source files , these source files can be an executable file, such should tset1.c, test2.c, test3.c three source file to produce an executable file, a command may be used as shown gcc achieve:

gcc -o test test1.c test2.c test3.c

If you want to execute the program, you only need to enter the following command line:

./test

Three .gcc basic usage

When using gcc compiler, we must give a series of necessary options and file names , so take an action in order to accurately gcc, gcc options and more than 100, many of the basic parameters will not be used, are described below some basic common usage.
gcc basic usage template shown below, where options is the parameter required for the compiler, gcc can be understood as an operation, and filename is the name of the relevant documents, objects representative of the operation:

gcc [ options ] [ filename ]

The following content options are the parameters:

1.-c

Means compile only, not linked into an executable file , the compile phase corresponds only to generate only the binary machine code test.o, the compiler does not normally contain a main subroutine file.

2. it

Means to generate an executable binary filetest , note the name of the output file is not the same as the source file, without stating the name of the output file, this time gcc will default name for the executable file output a.out, if subsequent re-compile other file, the a.outfile will be overwritten with new executable file output, so we try to use -oadd when output file name.

3.-g

-g is generated gdb debugger symbol information necessary , in order to debug the source code, you must add this option when compiling the program.

4.-O (uppercase)

It means a program optimizing compiler, linker, when using this option, the entire source code to optimize the compilation, linking process, the higher the efficiency of the executable file thus produced, but will reduce compile, link speed.

5.-O2

And the role of -O, are optimized, but the -O2 optimization stronger role , so the greater the degree of speed reduction.

6.-Wall

Meaning the output of all warning messages , in the course of the gcc compiler, gcc think that if an error has occurred in some places, it will put forward a number of warnings and tips.

7. Idirname

Named dirname directory is added to the header file directory listing program , which is used in the pretreatment stage option, I intended to include.

8.-Werror

Means all the warnings raised as an error , it is a good cause programmers attention.

IV. About optimization

About optimization, optimization can indeed lead to better execution performance to the program, but in some cases the following should be avoided optimized code:

  • Development: at the time of program development, optimize the higher the level, the more consumption compile time, so try not to use to optimize the development, when the software publisher or developer end, could be considered for the final code optimization.
  • Resources are limited: general optimization will increase the volume of executable code, the saying goes: the landlord did not surplus! So we try to reduce the use of resources in it.
  • Trace debug: when optimizing the code, you may excision some code, or to take to better performance and restructuring, this situation would become extremely difficult to trace and debug.
Published 62 original articles · won praise 188 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_43743762/article/details/100812197