Must-know gcc compilation


1. Workflow of gcc

  The gcc compiler generates an executable program from the c source file, and there are four steps in the middle:
insert image description here

  The four steps are not done independently by gcc, but internally call other tools to complete the entire workflow, among which compilation is the most time-consuming, because the syntax is checked line by line.
insert image description here

  • The following takes test.c as an example to introduce the four steps of gcc:
    gcc -E test.c -o test.i
    gcc -S test.i -o test.s
    gcc -c test.s -o test.o
    gcc test.o -o test
    generate the final executable program in one step:
    gcc test.c -o test

2. Common parameters of gcc

  • -v View gcc version number, --version can also
    insert image description here

  • -EGenerate preprocessing files

  • -Sgenerate assembly file

  • -cCompile only, resulting in .o files, usually called object files

  • -ISpecify the path where the header file is located

  • -LSpecify the path where the library file is located

  • -lSpecify the name of the library

  • -oSpecifies the name of the generated object file

  • -gContains debugging information, you need to add the -g parameter to use gdb to debug

  • -On n=0∼3Compile optimization, the larger the n, the more optimized

3. gcc compilation test

  • 1. Create a .c file:vim hello.c
    insert image description here

  • 2. gcc -E hello.c -o hello.i----- Expand the header file through the preprocessor, replace macros, and remove comments
    insert image description here

  • 3, gcc -S hello.i -o hello.s----- Make the C file into an assembly file through the compiler
    insert image description here

  • 4. gcc -c hello.s -o hello.o----- Convert the assembly file into a binary file through the assembler
    insert image description here

  • 5. gcc hello.o -o hello----- Combine the corresponding code in the function into the object file through the linker
    insert image description here

  • 6, ./hello----- Execute
    insert image description here

Summarize

  By learning to understand the process of gcc compilation, we briefly describe the complete process of running a program to the end:

1. Precompilation
  Precompilation mainly deals with precompilation instructions with # signs. The preprocessor removes comments, expands files, and replaces macros:

① Delete all #defines and expand all macro definitions.

② Delete all comments, // and /* */.

③ Process all conditional preprocessing instructions, such as "#if", "#endif", "#ifdef", "elif", "#else"

④ Process the #include precompiled instruction and replace the file content to its location, this process is recursive.

⑤ Keep all #pragma compiler directives, the compiler needs to use them, such as: #pragma once is to prevent the file from being referenced repeatedly.

⑥ Add line number and file identification to facilitate the generation of line number information for debugging during compilation, that is, the line number can be displayed when compilation errors or warnings occur.

2. Compile

  The precompiled .i or .ii file is subjected to a series of lexical analysis, semantic analysis, syntax analysis, and optimization through the compiler ccl to generate a .s assembly code file.

3. Compilation

  Convert the compiled assembly code file into an instruction (machine code file) that can be executed by the machine, and the object file generated by assembly is .obj under windows and .o under linux. This process is done by the assembler as.

4. Links

&emspl Links object files generated by different source files to generate an executable program.

Supplement:
linking is divided into static linking and dynamic linking:

Static link:

  Functions and data are compiled into a binary file, that is, all program modules are linked into a single executable file.

Advantages: run fast.

Disadvantage: It will cause a waste of space, because each executable program requires a copy of all required object files. If multiple programs depend on the same object file, multiple identical copies will be generated.

Dynamic link:

  The program is divided into relatively independent parts according to modules, and they are linked together to form a complete program when the program is running.

Advantages: Shared library, easy to update.

Disadvantage: At runtime, performance is not as good as static linking, since it requires linking every time it executes.


I look forward to your communication with me, leave a message or private message, learn together and make progress together!

Guess you like

Origin blog.csdn.net/CltCj/article/details/123603499