Gcc compiler can compile and connect source programs, assemblers and target programs in c and C++ languages into executable files. If the name of executable file is not given, gcc will generate a file named a.out In Linux system, executable files have no uniform suffix, and the system distinguishes executable files from non-executable files according to their attributes. Gcc distinguishes the categories of input files by suffixes. Here are some convention rules that gcc follows.
. C is the suffix file and the source code file of C language;
With the file. The suffix is an archive file composed of the target file;
Files with suffix. c,。 Cc or. Cxx is a C++ source code file;
Included files. H suffix is the header file contained in the program;
Included files. The I suffix is a preprocessed C source code file;
Included files. The suffix ii is the preprocessed C++ source code file;
Included files. The m suffix is an Objective-C source code file;
Included files. O suffix is the compiled target file;
A file. S suffix is an assembly language source code file;
A file with a suffix. S is a precompiled assembly language source code file.
Implementation process of Gcc
Although we call gcc a compiler for C language, the process of generating executable files from source code files of C language by using Gcc is not only a compilation process, but also involves four interrelated steps: preprocessing, compilation, assembly and linking.
Command gcc first calls cpp for preprocessing. During preprocessing, files contain and precompile statements (such as macro definitions, etc.). ) in the source code file for analysis. Then call cc 1 to compile. At this stage, with. The o suffix is generated from the input file. Assembly process refers to the steps of assembly language, calling as to work. Generally speaking, the source code file of assembly language is written in. Assembly language file with suffix. Suffixes all generate the target file. O suffix after precompilation and assembly. When all the target files are generated, gcc calls ld to complete the final key work, and this stage is the connection. In the connection stage, all the target files are arranged in the appropriate positions in the executable program, and the library functions called by the program are also connected to the appropriate places from their respective files.
Basic usage and options of Gcc
When using Gcc compiler, a series of necessary call parameters and file names must be given. There are more than 100 call parameters for Gcc compiler, most of which may not be used at all. Only the most basic and commonly used parameters are introduced here.
The basic usage of Gcc is:: gcc [options] [file name]].
Among them, options is the parameter needed by the compiler, and filenames gives the relevant file names.
-c, only compile, not link to become an executable file. The compiler only generates object files with suffixes. O from the input source code file such as. C, usually used to compile subroutine files that do not contain the main program.
-o output_filename, make sure that the name of the output file is output_filename, and this name cannot have the same name as the source file. If this option is not given, gcc will give the preset executable file a.out.
-g, generate the symbol information necessary for the symbol debugging tool (gdb of GNU). If we want to debug source code, we must add this option.
-O, optimize the compilation and linking of the program. Using this option, the whole source code will be optimized during compilation and linking, which can improve the execution efficiency of the generated executable file, but the speed of compilation and linking is correspondingly slow.
-O2 is better than -O in optimizing compilation connection. Of course, the whole compilation connection process will be slower.
-Idirname adds the directory indicated by dirname to the directory list of the program header file, which is a parameter used during precompiling. The header file in C program contains two situations:
A)# including
B)# contains "myinc.h"
Among them, Class A uses angle brackets (
-Ldirname adds the directory indicated by dirname to the directory list of program function archive files, which is a parameter used in the connection process. By default, the linker ld searches for the required archive file in the default path of the system (such as /usr/lib). This option tells the linker to search in the directory specified by -L first, and then in the default path of the system. If the function library is stored in multiple directories, you need to use this option to give the corresponding storage directories in turn.
-lname, when connecting, load the function library named "libname.a", which is located in the system preset directory or the directory determined by the -L option. For example, -lm means to connect a mathematical function library named "libm.a".
Above, we briefly introduced the most commonly used functions and main parameter options of gcc compiler. For more details, please refer to the online help of Linux system.
Suppose we have a C language source code file named test.c, and the easiest way to generate an executable file is:
Gcc test
At this time, precompilation and compilation connection are completed at one time, and an executable file named a.out is generated by system preset. For a slightly more complicated situation, such as having multiple source code files, needing to be connected to the archive or having other special requirements, appropriate call option parameters should be given. Look at another simple example.
The whole source code program consists of two files, testmain.c and testsub.c, in which the mathematical library provided by the system is used, and the executable file is expected to be test. At this point, the compilation command can be:
Gcc test main. c test sub c□lm□o test
Where -lm represents the mathematical library libm.a of the connection system.
Types and countermeasures of errors in Gcc
If the Gcc compiler finds an error in the source program, it cannot continue and cannot generate the final executable file. To facilitate modification, gcc gives an error message. We must analyze and deal with these error messages one by one, and modify the corresponding language to ensure the correct compilation and connection of the source code. The error messages given by gcc can generally be divided into four categories. Let's discuss the reasons and countermeasures respectively.
Class I: Class C grammatical errors
Error message: syntex error)C exists in the file source. This type of error is generally a syntax error in C language, so we should carefully check the programs before N lines in the source code file, and sometimes we need to check the header files contained in this file. In some cases, gcc will give many errors for a simple syntax error. The most important thing for us is to keep a clear head, don't be intimidated by it, and refer to the basic teaching materials of C language when necessary.
The second category: header file errors
Error message: the header file head.h cannot be found (including file head.h cannot be found). This error is a problem with the header file in the source code file. Possible reasons include wrong header file name, wrong directory name of specified header file, etc. , or incorrect use of double quotation marks and angle brackets.
The third category: archiving errors
Error message: The linker cannot find the required library, for example:
Ld: -lm: There is no such file or directory.
This error is an error in the function library connected to the target file. The possible reasons are the wrong name of the function library and the wrong name of the directory where the specified function library is located. The method of checking is to use the find command to find the corresponding function library name in the possible directories, determine the names of archives and directories, and modify the names in the program and compilation options.
Category 4: Undefined symbols
Error message: There is an undefined symbol. There may be two reasons for this error in the connection process: first, the source code file of the user-defined function or global variable is not compiled, connected or not defined at all, so the user needs to modify the source program according to the actual situation and give the definition body of the global variable or function; Secondly, the undefined symbol is a standard library function, which is used in the source program, but the name of the corresponding library is not given during the connection process, or there is something wrong with the name of the archived directory. At this time, we need to use the archive maintenance command ar to check which library function we need, and then modify the -l and -L items in the gcc connection options after confirmation.
Eliminating errors in compiling links should be said to be only the simplest and most basic step in programming, and it can be said to be just the beginning. The mistakes in this process are only those we make when describing an algorithm in C language, which are relatively easy to eliminate. We write a program until it is compiled and connected. It should be said that it has just begun. The problems in the process of program running are the problems of algorithm design. More specifically, we don't have enough knowledge and understanding of the problem, and we need more in-depth testing, debugging and modification. A program, a slightly more complicated program, often has to be compiled, linked, tested and modified many times. The program maintenance, debugging tools and version maintenance we learn below are all used in the process of program debugging and testing to solve the problems in the debugging stage. top of form
Form bottom