【Linux Tools】-gcc/g++

1. Introduction

We know that computers can only recognize binary, so how does the code we write run?
The code is compiled, linked, and finally an executable program is formed.
The compilation process includes: preprocessing, compilation, assembly, so what do these stages do?
Use gcc/g++ to introduce.

Second, the code translation process

1. Pretreatment

gcc -E test.c -o test.i

gcc -E test.c means: translate the program from now on, and stop after the preprocessing ends.

insert image description here

In order to prevent the preprocessed code from being directly printed to the screen, you can add the -o option, which means output to the specified file.

insert image description here
The work of the preprocessing stage

  • to comment
  • macro expansion
  • header file expansion
  • conditional compilation

Let's verify it:

insert image description here

You can see that there are macro definitions, conditional compilation, and comments in the test.c file, including header files, and
changes in file content after preprocessing:

insert image description here

2. Compile

During the compilation process, the compiler will convert the original C code into assembly code, and you can also use the gcc command to view the compiled file

gcc -S test.i -o test.s

insert image description here

3. Compilation

The assembly process will convert the assembly code into a relocatable binary file. Although a binary file is formed here, it cannot be executed without linking.

gcc -c test.s -o test.o

insert image description here

The reason for seeing the above scenario is that vim is a multi-mode text editor, so it parses the binary file in the form of text, and it presents the above situation.

4. Link

The linking stage is to link our program to the corresponding library, and finally generate a binary file in ELF format, that is, an executable program.
There are many functions in our program that depend on the implementation of the library, for example: the realization of printf, scanf and other functions.

Link

  • Static linking
    The compiler's linker will now link our program with the static library in the form of static linking.
  • Dynamic link
    The linker of the compiler will link our program with the dynamic library in the form of dynamic link at this time.

uselddThe command can view which dynamic libraries an executable program links to

ldd test

insert image description here

Under Linux: The dynamic library ends with .so, and the static library ends with .a.
Under Windows: The dynamic library ends with .dll, and the static library ends with .lib.

(1) Static library

When the program is linked with the static library, the code that uses a certain function in the library in the program you write is directly copied into your own program.
Once the link is successful, even if there is a problem with the static library, it will not affect the running of the program, but it also increases the size of the program.

(2) Dynamic library

When the program is linked with the dynamic library, it is to copy the address of a function code in the library that is used in the program written by oneself to the corresponding position of the program, and then go to the library to realize this function when the program is running.
Therefore, dynamic libraries are also calledshared library

(3) Comparison of dynamic and static libraries

  • The dynamic library is designed to allow the compiler to dynamically link the user's program, and the static library is designed to allow the compiler to statically link the user's program.
  • Dynamic linking is to copy the address of the code in the dynamic library to the corresponding location of the user program, and static linking is to copy the code in the static library directly to the user program.
  • After the static link is successful, it no longer depends on the library and can run independently.
  • After the dynamic link is successful, the program still depends on the library. Once the dynamic library is missing, the program cannot run.
  • Static link is a waste of space due to direct copying of code
  • Dynamic link saves space because everyone shares the dynamic library.
  • In the Linux environment, the default link is a dynamic link.

static link

gcc test.c -o test-static -static

insert image description here

It can be seen that the size gap between the executable file formed by the dynamic link and the static link is still very large.

How to install C/C++ static library on Centos7

  • C: sudo yum install -y glibc-static
  • C++:sudo yum install -y libstdc++ -static

Three, common options

-E only activates preprocessing, this does not generate a file, you need to redirect it to an output file
-S compiles to assembly language without assembly and linking
-c compiles to object code
-o file output to file
-static This option Use static link
-g to generate debug information for generated files . The GNU debugger can use this information.
-shared This option will try to use the dynamic library as much as possible, so the generated file is relatively small, but the system needs the dynamic library.
-O0
-O1
-O2
-O3 The 4 levels of compiler optimization options, -O0 means no optimization, -O1 is The default value, -O3 is the highest optimization level
-w does not generate any warning messages.
-Wall Generate all warning messages.

Guess you like

Origin blog.csdn.net/Djsnxbjans/article/details/128620400