[Linux] Basic development tools - gcc/g++ articles

insert image description here

Foreword :
 In the previous article, I introduced how to use vim . After learning vim, you can write code, but vim is just a text editor. To make our code run, we need to use The compilation tool I will introduce to you today : gcc/g++ . Among them, gcc is a compiler for C language, and g++ is a compiler for C++. They are the same in terms of usage, so today we mainly focus on gcc. Let me introduce their usage methods so that you can get started quickly.
 Compilation is mainly divided into four processes: preprocessing, compiling, assembling, and linking . The following four specific processes will be combined to introduce the use of gcc, and some marginal knowledge to improve our internal strength will be interspersed.

1. Pretreatment

  • The main functions of preprocessing mainly include macro replacement, header file expansion, conditional compilation, and comment removal .
  • Preprocessing directives are lines of code beginning with # .
  • instruction:gcc -E test.c -o test.i
  • -E: tells gcc to stop the compilation process after preprocessing is complete.
  • -o: Write the current compilation result into the test.i file, .iwhich is a preprocessed C source program (note: it is still a source program at this time).

insert image description here

1.1 Header file expansion

 The expansion of the header file is to copy the contents of the header file to the current source code, which means that the header file must exist in the system before compiling, so how do I know if it exists in the system? In fact, there is no need to worry at all. The header files are part of the development environment. In the Windows environment, the vs and dev we use are called integrated development environments, which integrate code writing and compiling. When we download these tools, A development kit will be selected, which is actually to download C-related header files and library files. The Linux environment is specially used by programmers, so in most Linux environments, things related to the development environment, such as: code editors, code compilers, header files/library files, etc., have been prepared for us in advance Now, we can start writing code directly. The directory is the default search path
/usr/include/ for gcc/g++ header files under Linux , and there are many header files related to development under this path.
insert image description here

1.2 Conditional compilation

 Conditional compilation seems to rarely appear when we usually write code, but its role must not be ignored. Presumably, when you download some software, there will be a community version, a professional version, etc. Generally speaking, the community version of the software will have fewer functions than the professional version. The few functions are cut out through conditional compilation. If there is no conditional compilation, then for each version, the manufacturer needs to write a corresponding code, which is very troublesome during maintenance, and there may be problems with the community version. Modified, but not for the professional version. But with conditional compilation, manufacturers only need to maintain one code from the beginning to the end. For the community version, they only need to conditionally compile the code of the professional version and cut out the corresponding functions.

Small Tips : The file obtained after preprocessing .iis still in C language, but it is cleaner than our source code.

Two, compile

  • At this stage, gcc first checks the standardization of the code and whether there are grammatical errors to determine the actual work to be done by the code. After the check is correct, gcc translates the code into assembly language.
  • instruction:gcc -S test.i -o test.s

insert image description here

3. Compilation

  • The assembly stage is to convert the assembly instructions in the compiled .sfile into a machine-recognizable binary. This binary file is also called a relocatable object binary file, or object file for short .
  • instruction:gcc -c test.s -o test.o

insert image description here

4. Links

  • The link stage is to link the object file and the library file to form an executable program
  • instruction:gcc test.o -o mytest

insert image description here
 Sometimes, we will refer to and call other external subroutines in the program, or use the functions provided by other software . At this time, we must add the function library in the compilation process. In this way, the compilation The compiler can link all the program codes with the function library to generate correct execution files.

4.1 What is a library?

 The concepts of libraries and library functions were mentioned above. To give a simple example: When you first learned C language, you must have used printffunctions to print a string of characters to the display. At that time, we only knew that #include <stdio.h>printf can be used as long as we write a sentence at the beginning of our code. Now we know that stdio.hit is a header file, which contains some declarations, because there are printffunction declarations in this header file, so after wrapping it, we can use the printf function. The specific implementation method of printf is actually placed in the library. It can be said that the library provides us with the implementation of the method. The library is actually the source file, after a certain translation, and then packaged, only providing a file for the user, not for us. Providing too many source files can also achieve the purpose of hiding source files . At the same time, the library also prevents programmers from inventing their own wheels. So printf here is what we call a library function. The linking stage is to link the target file compiled from the source code we wrote with the library, because we use the C language, so the default link is the C language standard library. A library is essentially a file that exists in a specific directory on the system . The vast majority of function libraries are placed /usr/libin /libthe directory.
insert image description here
 The figure above shows libc.so.6the standard library of the C language.

4.2 Classification of libraries

 There are two types of libraries: dynamic libraries and static libraries . In the Linux environment, the suffix of the dynamic library is .so, and the suffix of the static library is .a. In the Windows environment, the suffix of the dynamic library is .dll, and the suffix of the static library is .lib. All library files follow the same naming rules, namely: libname.后缀.xxx.
Small Tips : The gcc compiler will find the standard library of C by default, and it will link the object file obtained by compiling the source code we wrote with the library file. This is why gcc cannot compile C++ source files, because gcc looks for the C standard library by default, and it cannot find the C++ library.

4.3 How are object files and libraries linked?

 In general, links fall into two categories: dynamic links and static links .

4.3.1 Dynamic linking

 Linking the object file with the dynamic library is called dynamic linking . The dynamic library is like an Internet cafe. Anyone who wants to go online can go to this Internet cafe. That is: the dynamic library is shared by all programs, and is generally called a shared library . This means that only one dynamic library is enough, and it can meet the needs of all programs.
 The characteristics of dynamic library sharing make it impossible to lose the dynamic library , just like Internet cafes are blocked, and people cannot go online. Once the corresponding dynamic library is lost, not only one program will be affected, but multiple programs may not be able to run normally.

  • Instructions ldd 可执行程序, you can view the dynamic libraries that an executable program depends on.

insert image description here
 In Linux, an executable program is compiled into an executable program, and dynamic linking is preferred.

4.3.2 Static linking

 Linking an object file with a static library is called static linking . A static library is like a computer mall. When someone needs to go online, they will go to the computer mall to buy a dedicated computer for their own use only. When the compiler uses the static library for static linking, it will copy its own method to the target program, and the program will no longer depend on the static library in the future .

  • gcc test.c -o mytest-static -static
  • It -staticmeans to perform static linking, provided that there is a static library.
  • yum install -y glibc-static: Install the C static library

insert image description here
Gcc gives priority to dynamic libraries by default . If we do not have dynamic libraries but only static libraries, it is also possible. -staticThe essence is to change the priority. The process of linking is not necessarily pure dynamic linking or static linking, both can appear at the same time, but if the -staticoption is added, all links will be turned into static links.

  • file mytest: Check what link the executable program mytest uses.

insert image description here

4.4 Comparison of advantages and disadvantages of dynamic and static links

advantage shortcoming
dynamic library Effectively save resources (disk space, memory space, network space, etc.) Once missing, all programs cannot run
static library Does not depend on the library, the successfully compiled executable program can be executed independently, and there is no need to ask the outside to read the contents of the library function Large size, consumes more resources

insert image description here

五、Debug&&release

Debug is the developer mode, and the user finally uses the release . The code in Debug mode can be traced and debugged, because the executable program formed in Debug mode has debug information added in it. This means that the executable program obtained in Debug mode must be larger than the executable program obtained in release mode.
 The gcc compiler, by default, compiles executable programs in release mode. To compile executable programs in Debug mode, you need to add -goptions, as follows:

  • gcc test.c -o mytest-Debug -g

insert image description here

  • readelf -S mytest: Read the corresponding executable program in the form of segments.
  • readelf -S mytest-Debug | grep debug: Filter out segments related to Debug.

insert image description here


 Today's sharing is over here! If you think the article is not bad, you can support it three times in a row . Your support is the driving force for Chunren to move forward!
insert image description here

Guess you like

Origin blog.csdn.net/weixin_63115236/article/details/131668990