Introduction to GCC applications in Linux

Source: Internet
Author: User
Introduction to GCC applications in Linux-general Linux technology-Linux programming and kernel information. The following is a detailed description. When developing applications for Linux, the C language is used in most cases. Therefore, almost every Linux programmer is faced with the primary issue of flexible use of the C compiler. Currently, the most common C language Compiler in Linux is GCC (GNU Compiler Collection), which is a compilation system that complies with the ansi c standard in the GNU project, compile programs written in C, C ++, Object C, and other languages. Not only is GCC very powerful, but its structure is also extremely flexible. One of the most commendable points is that it can support a variety of languages through different front-end modules, such as Java, Fortran, Pascal, Modula-3 and Ada.

Openness, freedom, and flexibility are the charm of Linux, and this is reflected in the GCC through which programmers can better control the entire compilation process. When using GCC to compile a program, the compilation process can be divided into four stages:

Pre-Processing)

Compile (Compiling)

Assembly)

Link (Linking)

Linux programmers can end GCC at any stage of compilation according to their own needs, so as to check or use the output information of the compiler at this stage, or control the final binary file, so that you can prepare for future debugging by adding different numbers and types of debugging code. Like other commonly used compilers, GCC also provides flexible and powerful code optimization functions to generate code with higher execution efficiency.

GCC provides more than 30 warning messages and three warning levels to help enhance program stability and portability. In addition, GCC has made a lot of extensions to the Standard C and C ++ languages, which improves program execution efficiency, helps the compiler to optimize code, and reduces programming workload.

Starting with GCC

Before learning to use GCC, the following example can help users quickly understand the working principle of GCC and apply it to actual project development. First, enter the code shown in Listing 1 in a familiar Editor:

Listing 1: hello. c

# Include int main (void) {printf ("Hello world, Linux programming! \ N "); return 0 ;}

Run the following command to compile and run the program:

# Gcc hello. c-o hello
#./HelloHello world, Linux programming!

From the programmer's point of view, you only need to simply execute a GCC command, but from the compiler's point of view, you need to complete a series of very complicated work. First, GCC needs to call the Preprocessing Program cpp, which is responsible for expanding the macro defined in the source file, and inserting the content contained in the "# include" statement into it; then, GCC will call ccl and as to compile the processed source code into the object code. Finally, GCC will call the link program ld to link the generated target code into an executable program.

To better understand the working process of GCC, you can separate the above compilation process into several steps and observe the running results of each step. The first step is to pre-compile. The-E parameter allows GCC to stop the compilation process after preprocessing:

# Gcc-E hello. c-o hello. I

If you check the content in the hello. cpp file, you will find that the content of stdio. h is indeed inserted into the file, and other macro definitions that should be preprocessed are also processed accordingly. The next step is to compile hello. I as the target code, which can be done by using the-c parameter:

# Gcc-c hello. I-o hello. o

By default. the I file is considered as the C language source code after preprocessing. Therefore, the above command will automatically skip the preprocessing step and start the compilation process. You can also use the-x parameter to let GCC compile from the specified step. The last step is to link the generated target file to an executable file:

# Gcc hello. o-o hello

When the modular design is used for software development, the entire program is usually composed of multiple source files, and multiple compilation units are formed accordingly, using GCC can well manage these compilation units. Suppose there is a program consisting of two source files foo1.c and foo2.c. to compile them and generate the executable program foo, you can use the following command:

# Gcc foo1.c foo2.c-o foo

If more than one file is processed at the same time, GCC will continue to follow the preprocessing, compilation, and link processes. If we look into it, the above command is roughly equivalent to executing the following three commands in sequence:

# Gcc-c foo1.c-o foo1.o
# Gcc-c foo2.c-o foo2.o
# Gcc foo1.o foo2.o-o foo

It is a waste of time to compile a project that contains many source files using only one GCC command. Suppose there are 100 source files in the project that need to be compiled, and each source file contains 10000 lines of code. If you use only one GCC command as above to complete the compilation, then GCC needs to re-compile each source file and then connect all the files. Obviously, this wastes a lot of time, especially when a user modifies only one of the files, there is no need to re-compile each file, because many generated target files will not change. To solve this problem, the key is to use GCC flexibly and use tools like Make.
Related Article

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.