Compile code using gcc/g++ in Linux

1. Method shorthand

Direct compilation syntax: directly compile the text.c file or text.cpp file into a text file.

  • gcc text.c -o text // gcc-o text.c text
  • g++ text.cpp -o text // g++ -o text.cpp text
  • Note: -o must correspond to the target file (executable file).

2. Specific process

2.1 Preprocessing stage

The preprocessing process is the process of header file inclusion, comment removal, and macro replacement.
Preprocessing directives are lines of code beginning with #.

Preprocess the text.c file into a text.i file

  • The option of gcc -E text.c -o text.i
    -E is to let gcc stop compiling after the preprocessing is finished.

insert image description here
It can be seen from the observation that the preprocessed file has more than 800 lines, and the header file is included in it, and in the main, the specific comments have been removed.

2.2 Compilation phase

The compilation phase refers to turning the preprocessed code into assembly language.

Change text.c to text.s or text.i to text.s:

  • gcc -S text.i -o text.s
    -S: Compile from now on, and stop if the compilation is successful.

insert image description here

2.3 Compilation stage

Convert assembly language to binary code.

Turn the text.c/text.s file into a text.o file:

  • gcc -c text.s -o text.o
  • .o files are also called relocatable object files.
    -c: Translate the program from now on, and stop if the compilation is complete.

Use the od -x file command to view the machine code:
insert image description here

2.4 Linking stage

Convert the .s file generated in the assembly stage into an object file (executable file).

Link the text.o file into the executable text:

  • gcc text.o -o -text

insert image description here

2.4.1 Link details

Links: Dynamic Links and Static Links.
Dynamic linking is to fill in the address of the method I want in the library (dynamic library.so) into my executable program and establish an association. (Saving resources)
Static linking is to directly copy the methods in the library (static library.a) into our executable program. (occupies resources)


gcc or g++ uses the dynamic link compilation process by default.

  • View file link properties
  • file file name

insert image description here


  • The source program adopts the strategy of static linking
  • gcc text.c -o text-s -static
    -static: The generated object files are statically linked.
    insert image description here
    It is observed that the file sizes vary greatly.

In the C program, the implementation of the "printf" function is not defined, and there is only the declaration of the function in the "stdio.h" header file in the precompilation, and there is no implementation of the function, so how does the program run "printf" What about?
In fact, gcc defaults to dynamic linking. After the preprocessing puts the declaration of the printf function in the .i file, the linking stage can find the implementation of the function according to the declaration of the function (the address of the function definition), thereby realizing the linking process.
can uselddCommand to view the status of the program linking dynamic library:

insert image description here

The operation of gcc and g++ is the same, and the method of g++ only needs to replace gcc with g++.

Guess you like

Origin blog.csdn.net/weixin_45153969/article/details/132491586