GCC entry-general Linux technology-Linux programming and kernel information. The following is a detailed description. 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 generates a file named a. out. 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,
In general,. S is the suffix of the assembly language source code files and assembly,. s is the suffix of the assembly language files after pre-compilation and assembly are generated with. o as the suffix of the target file.
After all the target files are generated, gcc calls ld to complete the final key work. This stage is the connection. In the connection phase,
All target files are arranged in the proper location of the executable program, and the library functions called by the program are also connected to the appropriate location 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. The gcc compiler has more than 100 calling parameters,
Most of these parameters may not be used at all. Here we will 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.
-Wall: indicates the compile warning option. If gcc encounters something that it deems may cause an error during the compilation process, it will issue some corresponding warnings and prompts. We are prompted to pay attention to whether there are any errors caused by errors in this area.
-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 the header file in the current directory. The function of this option is to tell cpp that if the required file is not found in the current directory,
Go to the specified dirname directory. In program design, if we need to distribute the included files in different directories,
You 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..
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.
Gcc compilation instance
The best way to learn is practice. Next we will start a very simple Hello World instance. Using the vi skills we learned in the first course,
Edit an original file named hello. c with the following content:
# Include
Int main (int argc, char * argv [])
{
Char * pszStr = "Hello world ";
Printf ("Hello, world ");
}
(1) use the default method to compile the program
$ Hoyt> gcc hello. c
$ Hoyt>./a. out
After compiling with the default method, a. out Program is generated and then executed.
(2) Use the-Wall option to check whether warning information exists.
$ Hoyt> gcc-o hello. c-Wall
Hello. c: In function 'main ':
Hello. c: 5: warning: unused variable 'pszstr'
Hello. c: 8: warning: control reaches end of non-void function
Two warning messages. One is the variable * pszStr stated on the fifth line. We can delete it. The other is that the declarative function returns int type, but no return value.
(3) Use the-O2 option to view optimization results
$ Hoyt> gcc-o hello. c
$ Hoyt> gcc-o helloO hello. c-O2
$ Hoyt> ls-al
-Rwxr-xr-x 1 llebsdb dba 5209 April 14 15:57 hello
-Rw-r -- 1 llebsdb dba 126 April 14 15:53 hello. c
-Rwxr-xr-x 1 llebsdb dba 5193 April 14 15:57 helloO
We can see that different optimizations will lead to different file sizes (of course, they will also lead to different execution speeds)
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.