[Linux] The use and introduction of gcc compiler

Table of contents

1. Introduction to GCC

Two, the main components of GCC

Three, the workflow of GCC

Four, some important features and functions of GCC

Five, GCC commonly used compilation options

Six, the specific usage of GCC's input and output options

Seven, GCC reference documents


1. Introduction to GCC

  • GCC, the full name of GNU Compiler Collection, is an open source compiler collection developed and maintained by the Free Software Foundation. It is a cross-platform compiler suite capable of compiling multiple programming languages ​​such as C, C++, Objective-C, etc.
  • GCC was originally developed for the GNU operating system (Unix-like operating system), but has now been extended to run on multiple operating systems and hardware platforms, including Linux, Windows, macOS, BSD and other UNIX-like systems.
  • The main role of GCC is to compile source code into object code (machine code). It provides compiler front-ends in different languages ​​for parsing and processing source code in various programming languages. Then, GCC converts these source codes into executable machine code on the target platform or object code related to a specific platform through a series of compiler backends.

Two, the main components of GCC

  • Frontend (Frontend): It is responsible for processing the source code and converting it into an intermediate representation (intermediate code) . The front end processes the syntax and semantics of different programming languages ​​and converts them into a common intermediate representation called GIMPLE (GNU Middle End Intermediate Programming Language). Every programming language has a corresponding front end for parsing and processing the source code of that language.
  • Optimizer: It performs a series of optimization operations based on the intermediate representation generated by the front end . The optimizer improves program performance and efficiency by reorganizing code, eliminating redundant operations, extracting loops, and pruning unreachable code. The optimizer can also perform optimizations such as scheduling, vectorization, and inlining, so that the program can use computer hardware resources more effectively.
  • Backend: It converts the optimized intermediate representation of the optimizer into machine code or assembly code on the target platform . The backend generates low-level instruction sequences that match the specific requirements of the target hardware architecture. The backend completes the generation of object code, including processes such as register allocation, instruction selection, code scheduling, and basic block layout.
  • Linker (Linker): The linker is the last major component in GCC, which works after the compilation is complete. The linker is responsible for merging together multiple object and library files to produce the final executable or shared library file . The linker combines the various code and data segments and resolves issues such as symbol references and relocations. It can also perform some additional optimizations on the code, such as removing unused code and data.

Three, the workflow of GCC

The process by which GCC converts source code into executable files mainly includes the following stages:

  • Preprocessing (Preprocessing): Before compiling, GCC first preprocesses the source code. The preprocessor will process preprocessing directives starting with "#", such as #include, #define, etc. Its main task is to expand macro definitions, include header files, remove comments, etc. Preprocessed code no longer contains preprocessing directives, but replaces them with actual code, resulting in a preprocessed source code file.
  • Compilation: After preprocessing, GCC enters the compilation phase. The compiler translates the preprocessed source code into assembly code (Assembly Code). Assembly code is a processor-specific low-level code that uses a specific set of assembly instructions. The compiler performs operations such as lexical analysis, syntax analysis, and semantic analysis to convert the source code into a series of assembly instructions.
  • Assembly: The assembly code generated by compiled code is still low-level code related to a specific processor and needs to be further converted into machine language. This step is compilation. GCC calls the assembler (Assembler) to convert the assembly code into an object file (Object File). An object file is a binary file that contains machine code and related metadata (such as symbol table, relocation table).
  • Linking: The last step is the linker (Linker) to link the object file with the necessary library files to generate the final executable file. The linker is responsible for resolving symbol references in object files, matching them with symbol definitions in other object files and library files. It also handles relocation information, resolving cross-module symbolic references to actual memory addresses. The resulting executable file contains all the machine code and related metadata, and can be directly executed on the specific platform.

Four, some important features and functions of GCC

  • Multi-language support: GCC supports multiple programming languages, including C, C++, Fortran, Java, Ada, etc. It provides a separate front-end compiler for each language, so that it can compile and optimize according to the specific language specification.
  • Portability: GCC is available on multiple platforms including Linux, Windows, macOS, etc. It is widely used in various computer architectures such as x86, ARM, PowerPC, etc.
  • Highly optimized: GCC has a configurable optimization level, which can perform different degrees of code optimization according to the needs of the program. It provides a variety of optimization techniques, such as loop unrolling, constant folding, function inlining, etc., to improve code execution efficiency and performance.
  • Powerful warning mechanism: GCC has a rich warning mechanism, which can detect potential problems and errors in the code, and help developers find and solve problems early. Warning features can help to write more disciplined and safe code.
  • Debugging support: GCC works closely with the GNU debugger GDB to provide powerful debugging support. It can generate symbol tables and debugging information, allowing developers to use a debugger to trace program execution and debug errors.
  • Extensibility: GCC is extensible, allowing users to customize the behavior of the compiler. It provides a plug-in mechanism and compilation options for developers to add new compiler extensions, optimization techniques, and target machine descriptions.
  • Library support: GCC supports the use of standard and third-party libraries. It has its own runtime library (glibc), which provides a rich package of library functions and system calls. In addition, GCC can also link other commonly used libraries, such as math libraries, graphics libraries, etc.
  • Static and dynamic linking: GCC supports static linking and dynamic linking. Programs and library files can be statically linked into an independent executable file, or dynamically linked to a runtime shared library.

Five, GCC commonly used compilation options

  • Input and output options:
    • -o <file>: Specifies the name of the generated executable or object file.
    • -E: Only preprocessing is performed, and the preprocessing results are output to standard output.
    • -S: Compile only and output assembly code to standard output.
    • -c: Compile only, not link.
  • Search path options:
    • -I<path>: Specifies the header file search path.
    • -L<path>: Specifies the library file search path.
    • -l<library>: Link the specified library file. For example: -lmmeans to link the math library libm.
  • Warning options:
    • -Wall: Enables all warning messages.
    • -Werror: Treat warnings as errors and terminate the compilation process.
    • -Wextra: Enables additional warning messages.
    • -Wpedantic: Enable stricter warnings, conforming to the strict semantics of the standard.
  • Optimization options:
    • -O0: Disable optimization.
    • -O1: Enable basic optimization.
    • -O2: Enables a higher level of optimization, including some less expensive ones.
    • -O3: Enables further optimizations that may result in increased compile times.
    • -Os: Optimize for size.
    • -Ofast: Enables more aggressive optimizations that may break some of the standard's strict semantic rules.
  • Standard options:
    • -std=<standard>: Specifies the version of the C or C++ standard to follow.
  • Multithreading options:
    • -pthread: Use the threading library. Enables support for multithreaded programming.
  • Macro definition options:
    • -D<macro>: Define a macro. -D<macro>[=<value>]Macros with optional values ​​can be defined using format .
  • Memory detection options:
    • -fsanitize=<options>: Enable the memory detection option.
    • address: Enable address access error detection.
    • leak: Enable memory leak detection.
  • CPU architecture options:
    • -march=<arch>: Set the CPU architecture of the target processor.
  • Target platform options:
    • -march=: Specifies the processor architecture of the target platform. For example: -march=armv8-a.
    • -mcpu=: Specify the processor model of the target platform. For example: -mcpu=cortex-a53.
  • other options:
    • -M<file>: Makefile dependencies.
    • -f<feature>: Turn on or off the specified compiler features.

Six, the specific usage of GCC's input and output options

  • Compile and generate an executable file: the following command is to myprog.ccompile it into an executable filemyprog
    • gcc -o output_file input_file.c
    • Multi-file compilation: If the program consists of multiple source files, they can be compiled together
    • gcc -o output_file input_file1.c input_file2.c
  • Perform preprocessing and output to a file: the following command is to myprog.cpreprocess and output the preprocessing result to myprog.ia file.
    • gcc -E input_file.c -o output_file.i
  • Compile and output assembly code: The following command will myprog.conly compile to assembly code and output the assembly code to myprog.sa file.
    • gcc -S input_file.c -o output_file.s
  • Compile and generate object files: the following command will myprog.ccompile to object filesmyprog.o。
    • gcc -c input_file.c -o output_file.o
  • Compile the object file to generate an executable file: the following command will myprog.ccompile to the object filemyprog。
    • gcc input_file.o -o output_file
  • Specify the header file search path: The following command will /usr/local/libsearch the directory for library files, link the math library -lm, and compile it myprog.cinto an executable myprog.
    • gcc -I/usr/local/include input_file.c -o output_file
  • Specify the library file search path and link library file: The following command will /usr/local/libsearch the library file in the directory, link the math library -lm, and then compile it myprog.cinto an executable file myprog.
    • gcc -L/usr/local/lib -lm input_file.c -o output_file

Seven, GCC reference documents

  • GNU Programming Guide : This guide is specific to GCC and covers topics such as compiler options, the compilation process, debugging and optimization.
  • GCC Wiki : This wiki page provides a wealth of information about GCC, including option explanations, examples, and best practices.
  • GNU Compiler Documentation : This is the official document of gcc, which contains detailed descriptions of various options and usage of gcc.
  • Linux man manual: man gccYou can view the man page of gcc by typing in the terminal.

Guess you like

Origin blog.csdn.net/weixin_43729127/article/details/132207579