Introduction to GCC

Source: Internet
Author: User

This section describes the C compiler-GCC in Linux, which is introduced by GNU. It mainly introduces the basic principles and usage of this compiler, as well as the causes and countermeasures of Errors generated during compilation.

Introduction to GCC

In Linux, GCC (gnu c compiler) is a powerful and superior multi-platform compiler launched by GNU. It is one of the representative works of GNU. GCC is a super compiler that can compile executable programs on multiple hardware platforms. Its execution efficiency is 20% higher than the average efficiency of general compilers ~ 30%.

The GCC compiler can compile and connect the C and C ++ language source programs, programming sequences, and target programs into executable files. if the name of the executable file is not given, GCC will generate. out file. In Linux, the executable file does not have a uniform suffix. The system distinguishes the executable file from the unexecutable File Based on the file attributes. GCC uses suffixes to differentiate the categories of input files. Next we will introduce some agreed rules of GCC.

. C is a suffix file, C language source code file;

. A is a file with a suffix. It is a file library consisting of the target file;

. C,. cc or. cxx is a C ++ source code file;

. H is a suffix file, which is the header file included by the program;

. I is a file with a suffix. It is a pre-processed C source code file;

. II is a file with a suffix. It is a pre-processed C ++ source code file;

. M is a suffix file, which is an objective-C source code file;

The. o file is the compiled target file;

. S is a suffix file, which is an assembly language source code file;

. S is a precompiled assembly language source code file.

GCC Execution Process

Although we call gcc a c language compiler, the process of using GCC to generate executable files from C language source code files is not just a compilation process, instead, we need to go through four interrelated steps: preprocessing (also called preprocessing), compilation, assembly, and linking ).

The command GCC first calls CPP for preprocessing. During the preprocessing process, it analyzes the file inclusion (include) and pre-compiled statements (such as macro definition define) in the source code file. Next, call the "cc0" command to compile the file. At this stage, the target file with the. O suffix is generated based on the input file. The assembly process is a step for the assembly language and calls as for work. Generally ,. S is the suffix of the assembly language source code file and assembly ,. the assembly language files suffixed with S are generated after pre-compilation and assembly. O is the target file with the suffix. After all the target files are generated, GCC calls LD to complete the final key work. This stage is the connection. During the connection phase, all target files are arranged in the proper location of the executable program. At the same time, the library functions called by this program are also connected to appropriate places from their respective archives.

Basic GCC usage and options

When using the GCC compiler, we must provide a series of necessary call parameters and file names. There are more than 100 GCC compiler call parameters, most of which may not be used at all. Here we only introduce the most basic and commonly used parameters.

The most basic usage of GCC is: GCC [Options] [filenames]

Among them, options is the parameter required by the compiler, and filenames provides the relevant file name.

-C: only compiled, not connected to executable files, the compiler is only input. C and other source code file generation. O is a target file with a suffix. It is usually used to compile a subroutine file that does not contain the main program.

-O output_filename: Make sure the output file name is output_filename. The name cannot be the same as the source file name. If this option is not provided, GCC provides the preset Executable File A. Out.

-G, generate the symbolic information necessary for the symbolic debugging tool (gnu gdb). To debug the source code, we must add this option.

-O: optimizes compilation and connection of programs. With this option, the entire source code is optimized during compilation and connection. This improves the execution efficiency of executable files, however, the compilation and connection speed is correspondingly slower.

-O2: it is better to optimize compilation and connection than-o. Of course, the entire compilation and connection process will be slower.

-Idirname: add the directory specified by dirname to the program header file directory list, which is a parameter used during the pre-compilation process. The header file in the C program contains two situations:

A) # include

B) # include "myinc. h"

Class A uses angle brackets (<>) and Class B uses double quotation marks (""). For Class A, the pre-processing program CPP searches for corresponding files in the system preset include file directory (such as/usr/include). for Class B, CPP searches for header files in the current directory, the purpose of this option is to tell CPP that if the desired file is not found in the current directory, it will be searched in the specified dirname directory. In programming, if we need to distribute the included files in different directories, we need to use the-I option to provide the search path one by one.

-Ldirname: adds the directory specified by dirname to the directory list of the file in the program function file. It is a parameter used during connection. In the default state, The Connection Program LD searches for the required file library in the system's preset path (for example,/usr/lib). This option tells the Connection Program, first, go to the directory specified by-l, and then find it in the system preset path. If the Function Inventory is placed in multiple directories, you need to use this option in sequence, the corresponding storage directory is provided.

-Lname: During connection, load the function library named "libname. A", which is located in the preset directory of the system or the directory determined by the-L option. For example,-LM indicates connecting the mathematical function library named "libm..

The above briefly introduces the most common functions and Main Parameter options of the GCC compiler. For more information, see the online help of Linux.

Suppose we have a c-language source code file named test. C. to generate an executable file, the simplest method is:

GCC test. c

At this time, the pre-compilation and compilation connections are completed at one time, and a preset name named a is generated. for a slightly complicated out executable file, such as multiple source code files, the need to connect to the file library, or other special requirements, appropriate call option parameters must be given. Let's look at a simple example.

The entire source code program consists of two files: testmain. C and testsub. c, the program uses the mathematical library provided by the system, and the executable file to be given is test, then the compile command can be:

GCC testmain. c testsub. C-lm-o Test

Here,-LM indicates the Math Library libm. A connected to the system. The process can be described in Figure 12-1.

GCC error types and Countermeasures

If the GCC compiler finds an error in the source program, it cannot continue or generate the final executable file. To facilitate modification, GCC provides error information. We must analyze and process these error information one by one, and modify the corresponding language to ensure the correct compilation and connection of source code. The error information provided by GCC can be divided into four categories. The following describes the causes and countermeasures.

Class 1: C syntax error

Error message: line N of the file source. C contains a syntax error (Syntex errror ). This type of error is generally a C language syntax error. You should carefully check the nth line in the source code file and the program before this line. Sometimes you need to check the header file contained in this file. In some cases, a very simple syntax error occurs. GCC will give a lot of errors. The most important thing is to keep a clear mind and not be intimidated by it, if necessary, refer to the basic C language teaching materials.

Category 2: header file Error

Error message: the header file head. H (can not find include file head. h) cannot be found ). This type of error occurs when the source code file contains a header file, which may be caused by incorrect header file name or directory name of the specified header file, or double quotation marks and angle brackets.

Category 3: Archive errors

Error message: the connection program cannot find the required function library, for example:

Ld:-LM: no such file or directory

This type of error occurs in the function library connected to the target file. The possible cause is that the function library name is incorrect, and the Directory Name of the specified function library is incorrect, the check method is to use the find command to find the corresponding function library name in a possible directory, determine the name of the file library and directory, and modify the name in the program and compilation options.

Category 4: undefined symbols

Error message: Undefined symbol ). This type of error occurs during the connection process. There may be two reasons: first, the source code file of the function or global variable defined by the user is not compiled or connected, or there is no definition yet. This requires the user to modify the source program based on the actual situation and give the definition body of the global variable or function. Second, the undefined symbol is a standard library function, the library function is used in the source program, but the name of the corresponding library is not given during the connection, or the Directory Name of the library is incorrect, in this case, you need to use the archive maintenance command ar to check which function library the library function is in. After confirming, modify the-L and-l items in the GCC connection options.

Excluding errors during compilation and connection, it should be said that this is only the simplest and most basic step in program design. Errors in this process are only the errors that we encounter when using C language to describe an algorithm. They are easier to eliminate. Let's write a program until the compilation and connection are passed. It should be said that at the beginning, the problems encountered during the program running are problems in algorithm design, A more mysterious point is that we do not have enough understanding of the problem and need to perform further testing, debugging, and modification. A program, a slightly complex program, often needs to be compiled, connected, tested, and modified multiple times. The program maintenance, debugging tools, and version maintenance we will learn below are used in the program debugging and testing process to solve the problems encountered during the commissioning phase.

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.