[Linux series P5] The wonderful encounter between gcc&g++ and [dynamic and static libraries]

foreword

  • Hello everyone, welcome to the YY drop Linux series, a warm welcome!
  • The main content of this chapter is for veterans who have been in contact with Linux. The main content includes

Welcome to subscribe to the YY drop Linux column! More dry goods are continuously updated! The following is the portal! 

Subscribe to the column reading: YY's "Linux" series❀❀❀❀❀
【Linux Series-P1】Building a Linux Environment
[Linux Series-P2] Basic knowledge and instructions of Linux
【Linux Series-P3】Linux Permissions
[Linux series-P4] Linux basic tools [yum] [vim]

Table of contents

 

1. Low-level knowledge points

2. Function library

1. The basics of the library

2. The naming rules of the library (you can recognize it)

3. Use of Linux compiler - gcc/g++

1. What is gcc/g++?

2. What is the effect of gcc/g++?

3. How to use gcc/g++? What are the options? How to remember?

4. Use the g++ command to control each process of [translation]

1. Preprocessing (macro replacement)

2. Compile (generate assembly)

3. Assembly (generating machine-readable code)

4. Link (generate executable file or library file)

5. [.obj file] link with the library

1. [Dynamic Library/Dynamic Link] and [Static Library/Static Link]

2. Compile to form an executable program, and [Dynamic Link] is used by default 

3. [-static] option & [dynamic and static library details]


1. Low-level knowledge points

Introduction: When we develop C/C++ and other forms on Window or Linux systems, we may have such questions-how is this process realized?

  • In fact, in our system, there are header files and library files related to C++ or other language development in advance ;
  • In other words, the C++ development environment not only refers to vscode, gcc, g++, but more importantly, whether the system contains the header files and library files of the language itself
  • When we install a compiler such as vscode, we will find that it will let us choose the corresponding development package , including C header files and library files

2. Function library

1. The basics of the library

Introduce:

  • In our C program, the function implementation of "printf" is not defined, and the "stdio.h" included in the precompilation only has the declaration of this function, but does not define the implementation of the function , so where is the implementation " printf" function?
  • The final answer is: the system implements these functions in the library file named libc.so.6. If there is no special designation, gcc will go to the system default search path "/usr/lib" Find, that is, link to the libc.so.6 library function, so that the function "printf" can be realized, and this is the function of the link
  • The process of linking occurs in the preprocessing stage, and the role of [library] is to provide the implementation of linking methods ;
  • The library, in fact, is to take multiple source files (.c files), after a certain translation, and then package them——in the end, only one file is provided to us ;
  • The standard library of C language is essentially a file with a path;
  • We can get such an equation: our software = our code + the realization of the method provided by the library file;

2. The naming rules of the library (you can recognize it)

Introduction: On our machine, the dynamic library is installed by default , and the static library is not installed by default

Example: We can find file names like this - lib name .so .XXX 

Linux

  • .so (dynamic library)
  • .a (static library)

Windows

  • .dll (dynamic library)
  • .lib (static library) 

3. Use of Linux compiler - gcc/g++

1. What is gcc/g++?

  • gcc is the GNU Compiler Collection, formerly known as the Gun C Language Compiler, because it originally could only handle the C language , but gcc quickly expanded to include many compilers (C, C++, Objective-C, Ada, Fortran, Java), It can be said that gcc is the GNU compiler collection;
  • Points to note: g++ can handle both C/C++ language, while gcc can only handle C language ; generally we can use g++;

2. What is the effect of gcc/g++?

  • Function: gcc/g++ is to compile (preprocess, compile, assemble, link) the text file containing the code into an executable file. Then we can execute it; for example, we wrote a piece of code (named a.cpp), you can use any text editing software to write it , you don’t need to be an IDE

3. How to use gcc/g++? What are the options? How to remember?

gcc [选项] 要编译的文件 [选项] 目标文件

在下文中有关于选项更详细的实操

gcc options:

  • -E only activates preprocessing, this does not generate files, you need to redirect it to an output file
  • -S compile to assembly language do not assemble and link
  • -c compile to object code
  • -o file output to file
  • -static This option uses static linking for the generated files
  • -g Generate debugging information. The GNU debugger can use this information.
  • -shared This option will 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, and -O3 has the highest optimization level
  • -w Do not generate any warning messages.
  • -Wall Generate all warning messages.

Memory Tips for Options

  •  esc, iso
  • [ESC key in the upper left corner of the keyboard], [two iso behind the ios system]

4. Use the g++ command to control each process of [translation]

Introduction: The principle of g++ control process can be understood as a cut-off valve , (.c file) from now on to translate the program

  • -E means stop after the preprocessing work is done, and generate (.i file)
  • -S means stop the compilation work and generate (.s file)
  • -c means to stop after the assembly work is completed and generate (.o file)

1. Preprocessing (macro replacement)

  • Preprocessing functions mainly include macro definition, file inclusion, conditional compilation (to get different versions of software), and comment removal .
  • Preprocessing directives are lines of code that begin with the # sign.
  • Example: 
    gcc –E hello.c –o hello.i
  • Option "-E", the function of this option is to let gcc stop the compilation process after the preprocessing is completed.
  • The option "-o" is [point to the target file] , and the ".i" file is the original C program that has been preprocessed.

2. Compile (generate assembly)

  • In this stage, gcc first needs to check the standardization of the code, whether there are grammatical errors, etc., to determine the actual work to be done in the code, and after the check is correct, gcc translates the code into assembly language.
  • Users can use the "-S" option to view, this option only compiles but not assembles, and generates assembly code.
  • Example: 
    gcc –S hello.i –o hello.s

3. Assembly (generating machine-readable code)

  • The assembly stage is to convert the ".s" file generated in the compilation stage into an object file
  • Readers can use the option "-c" to see that the assembly code has been converted into " .o" binary object code
  • (.o file) object file is (.obj file), (.obj file) cannot be executed independently, but also needs to be linked
  • Example: 
    gcc –c hello.s –o hello.o

4. Link (generate executable file or library file)

  • After successful compilation, it enters the linking phase
  • (.o file) and library (dynamic library/static library) link to form an executable program
  • Example: 
    gcc hello.o –o hello

5. [.obj file] link with the library

1. [Dynamic Library/Dynamic Link] and [Static Library/Static Link]

  • On the contrary, the dynamic library does not add the code of the library file to the executable file when compiling and linking, but is linked by the runtime [file loading library] when the program is executed , which can save system overhead. The general suffix of the dynamic library is ".so" , as mentioned above, libc.so.6 is the dynamic library. gcc uses dynamic libraries by default when compiling. After the link is completed, gcc can generate the executable file, as shown below.
     gcc hello.o –o hello
  • The static library means that when compiling and linking, all the code of the library file is added to the [executable file] , so the generated file is relatively large, but the library file is no longer needed at runtime. The suffix is ​​generally ".a"

2. Compile to form an executable program, and [Dynamic Link] is used by default 

  • In Linux, the binary program generated by gcc by default is dynamically linked , which can be verified by the file command

3. [-static] option & [dynamic and static library details]

  • In Linux, if you want to form an executable program in the way of static linking , you need to add [ -static option] (providing a static library), for example
gcc mycode.c -o mycode -static
  • But if we don't have a static library , we must force -static, which is impossible ; from this we can see that the essence of -static: change the priority of linking (.o files) with the library, priority: dynamic library > static library;
  • Not necessarily a pure full dynamic/static library, generally a mixture of dynamic and static libraries;

Guess you like

Origin blog.csdn.net/YYDsis/article/details/131352391