GCC compilation of c procedures method and process analysis

  • 2020-04-02 02:08:09
  • OfStack

At present, the most commonly used C language Compiler under Linux is GCC (GNU Compiler Collection), which is a compilation system in accordance with the ANSI C standard in GNU project and can compile programs written in C, C++, Object C and other languages. Not only is GCC very powerful, but its structure is also extremely flexible. The best part is that it supports a variety of languages, such as Java, Fortran, Pascal, modula-3, and Ada, with different front-end modules. Openness, freedom, and flexibility are part of the beauty of Linux, and GCC gives programmers more control over the compilation process. When compiling programs using GCC, the compilation process can be broken down into four phases:

pretreatment (Pre - Processing)
compile (the Compiling)
assembly (Asse mbling)
link (Linking)
Linux programmers can end GCC at any stage of compilation to their own needs, either to check or use the compiler's output at that stage, or to control the resulting binaries to prepare for future debugging by adding different amounts and types of debugging code. Like other commonly used compilers, GCC provides flexible and powerful code optimizations to generate more efficient code.
GCC provides more than 30 warning messages and three warning levels, which can be used to enhance program stability and portability. In addition, GCC also makes a lot of extensions to the standard C and C++ languages to improve the execution efficiency of programs, help the compiler to optimize the code, and can reduce the programming workload.

C program compilation process is mainly divided into four stages: pre - processing, the compiling, assembling, and linking;
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201401/201401302213262.gif ">

Suffixed names of commonly used files:
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201401/201401302213263.gif ">
GCC pre-processing phase: mainly includes header files (# include) and macro definitions (# define,#ifdef... ) handle. You can use "gcc-e" to have GCC stop compiling 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 normalcy, syntax errors, etc. To determine what the code actually does, GCC translates the code into assembly language after checking for errors. The user can view it using the -s option, which only goes in
Line compilation instead of assembly, generating assembly code.
[root@localhost GCC]# gcc-s hello. i-o hello. S
GCC assembly phase: generate object code *.o; There are two ways: using GCC to generate the object code gcc-c *.s-o *.o directly from source code and using an assembler to generate the object code as *.s-o *.o from assembly code
[root@localhost GCC]# gcc-c hello. s-o hello.o
[root@localhost GCC]# as hello.s-o hello.o
You can also directly use as *. S to perform the assembly and link process to produce the executable a.out, which you can specify as the output file format using the -o option above.
GCC link phase: generate executable files; Executable files can be generated in the following format: a.out/*/, and possibly in other formats as well.
[root @ localhost GCC] # GCC hello. O Generate the executable a.out
[root@localhost GCC]# GCC hello.o-o hello Generate the executable hello
GCC commonly used compilation options:
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201401/201401302213264.gif ">
-dmacro defines a specified macro that can be checked by #ifdef in the source code.

-o, -o2, -o3 will open the optimization state, this option cannot be used in combination with the -g option;
-v activates all alerts and prints information about the compilation process;
-Wall cancels compilation when an alert occurs, which is considered an error;
-werror cancels the compilation operation when the alarm occurs, that is, the alarm is regarded as an error;
-w no alarms.
Use of GCC link library files
When developing software under Linux, it is relatively rare to use no third-party function library at all, and it is usually necessary to rely on the support of one or more function libraries to complete the corresponding functions. From a programmer's perspective, a library is really just a collection of header files (.h) and library files (.so or.a). While most functions under Linux default put header files under /usr/include-/ and library files under /usr/lib/, this is not always the case. Because of this, GCC must have its own way of finding the required header and library files at compile time. GCC searches directories to find the files it needs, and the -i option adds a new directory to GCC's header search path. For example, if you have the header files needed at compile time under /home/justin/include/, you can use the -i option to make it easy for GCC to find them:
C -I /home/justin/includ-o foo
Similarly, if a library file is not in the standard location, you can add a new directory to GCC's library file search path with the -l option. For example, if there is a link in the /home/xiaowp/lib/ directory to the library file libfoo.so that GCC can find it successfully, you can use the following command:
C -L /home/justin/lib-lfoo -o foo
Worth explaining is the -l option, which instructs GCC to connect to the library file libfoo.so.
Linux library files in the naming of a convention, that is, should start with lib three letters, because all library files follow the same specification, so when using the -l option to specify the library name of the link can be omitted lib three letters, that is, GCC in the -lfoo processing, will automatically link to the name of libfoo.so
Library files under Linux are divided into two categories: dynamically linked libraries (usually ending in. So) and statically linked libraries (usually ending in. By default, GCC prioritized using dynamic link libraries when linking, and only considered using static link libraries when dynamic link libraries did not exist. If necessary, you can add the -static option at compile time to force the use of static link libraries. For example, if the library files libfoo.so and libfoo.a are required for linking under the directory home/ Justin /lib/, in order for GCC to only use the static link library when linking, use the following command:
C -L /home/ Justin /lib -static -lfoo -o foo The file.
For the dynamic and static library file creation methods, not explained here, you can refer to another Linux c library file creation method.


Related articles: