The most common C-language compiler currently available under Linux is GCC (GNU Compiler Collection), a compiler system that complies with the ANSI C standard in the GNU Project and compiles programs written in languages such as C, C + +, and Object C. GCC is not only very powerful, but also extremely flexible in structure. The most commendable thing is that it can support various languages through different front-end modules, such as Java, Fortran, Pascal, Modula-3 and Ada. Openness, freedom and flexibility are the charms of Linux, and this is reflected in GCC that allows programmers to better control the entire compilation process. When compiling a program with GCC, the compilation process can be subdivided into four phases:
pretreatment (pre-processing)
compiling (compiling)
compilation (Asse mbling)
links (linking)
Linux programmers can let GCC end at any stage of the compilation to their own needs, to check or use the compiler's output information at that stage, or to control the resulting binaries to prepare for future debugging by adding different numbers and kinds of debugging code. Like other commonly used compilers, GCC provides a flexible and powerful code optimization feature that can be used to generate more execution-efficient code.
GCC provides more than 30 warning messages and three warning levels that can be used to enhance program stability and portability. In addition, GCC expanded the standard C and C + + languages to improve the execution efficiency of the program, helping the compiler to optimize code, and reduce the amount of programming effort.
C Program compilation process is divided into four stages: pre-processing,compiling,assembling,linking;
Suffix name for commonly used files:
gcc preprocessing phase: primarily for included header files (#include) and macro definitions (#define, #ifdef ...) ) for processing. You can use "gcc-e" to let GCC stop the compilation process after preprocessing and generate *.I files.
[root@localhost gcc]# gcc-e hello.c-o hello.i
GCC compilation phase: GCC first checks the code for syntax errors, and so on. To determine what the code actually does, GCC translates the code into assembly language after the check is correct. Users can view with the-s option, which only enters
line compilation without assembly, generating assembly code.
[root@localhost gcc]# gcc-s hello.i-o Hello.s
GCC assembly phase: Generate target code *.O; Use GCC to generate target code directly from source code GCC -C *.s-o *.O and using the assembler to generate the target code as *.s-o *.o
[root@localhost gcc]# gcc-c hello.s-o the hello.o
[root@localhost gcc]# as Hello.s-o hello.o
can also directly use as *.S, which will execute the assembly, link process to generate executable a.out, which can be used as above with the-O option to specify the format of the output file.
GCC link phase: Build executable file; The executable format that can be generated is: a.out/*/, and of course, there may be other formats.
[root@localhost gcc]# gcc hello.o build executable a.out
[root@localhost gcc]# gcc Hello.o-o Hello build executable Hello
gcc common compilation options:
-dmacro Defines the specified macro so that it can be validated through the #ifdef in the source code;
-O,-o2,-o3 to open the optimization state, which cannot be used in conjunction with the-G option;
-V To start all alerts, print the compilation process information;
-wall cancels the compile operation when an alert occurs, and sees the alert as an error;
-werror cancels the compile operation when an alert occurs, that is, the alarm is mistaken;
-W prohibits all alarms.
Use of GCC link library files
When developing software under Linux, it is rare to use no Third-party function library at all, and it usually requires the support of one or more function libraries to complete the corresponding function. From a programmer's point of view, a function library is actually a collection of header files (. h) and library files (. So or. a). Although most functions under Linux have the default to place headers in the/usr/include/directory, the library files are placed in the/usr/lib/directory, but not in all cases. Because of this, GCC must have its own way of compiling the header and library files needed at compile time. GCC uses the search directory to find the files that are needed, the-I option to add a new directory to GCC's header file search path. For example, if you have the header files that are required for compilation in the/home/justin/include/directory, you can use the-I option in order for GCC to find them successfully:
# gcc foo.c-i/home/justin/include-o foo
Similarly, if you use a library file that is not in a standard location, you can add a new directory to the GCC search path through the-l option. For example, if you need a library file to libfoo.so a link in the/home/xiaowp/lib/directory, you can use the following command in order for GCC to find it successfully:
# gcc foo.c-l/home/justin/lib-lfoo-o foo
What is worth explaining is the-l option, which instructs GCC to connect to the library file libfoo.so.
The library file under Linux has a convention for naming, that is, you should start with the Lib three letter, because all library files follow the same specification, so you can omit the Lib three letters when you specify a linked library file name with the-l option, which means that when GCC processes the-lfoo, will automatically go to the link named libfoo.so
The library files under Linux are divided into two main classes, namely, dynamic link libraries (usually. so end) and static link libraries (usually ending with. A), the difference is whether the code needed to execute the program is dynamically loaded at runtime or statically loaded at compile time. By default, GCC takes advantage of dynamic link libraries when linking, and only consider using a static link library if the dynamic link library does not exist, and enforces the use of static link libraries if necessary by adding the-static option at compile time. For example, if you need a library file libfoo.so and libfoo.a when there is a link in the home/justin/lib/directory, you can use the following command in order for GCC to use only static link libraries when linking:
# gcc foo.c-l/home/justin/lib-static-lfoo-o fooof the file.
For dynamic libraries and static library file creation methods, this is not explained in detail here, you can refer to another Linux C library file creation method.