[Linux] Linux compiler - use of gcc/g++

This blog needs to use the vim compiler. If you are not familiar with it, you can check the use of vim in this blog.

1. Background

To generate an executable file from a source file needs to go through the translation environment of the program (translate the text file of the program into a binary file that the machine can recognize), and the translation environment can be divided into the following four steps:

  1. Preprocessing (header file replacement, conditional compilation, macro replacement, decommentation, etc.)
  2. compile (generate assembly language)
  3. Assemble (generates a machine-readable binary, not executable, bin.obj)
  4. Linking (some kind of merging of our own .obj files and library files to generate executable files)

For details, please see this blog program environment .

gcc/g++ can help us generate C/C++ executable files under LInux, and execute four different steps to the translation environment through options.
gcc compiles C language codes, and g++ compiles C++ codes. Both are used in the same way. The following uses gcc as an example.

Two. How to generate gcc

Format:gcc [选项] 要编译的文件 [选项] [目标文件]
We first create a hello.c test file, and use the vim editor to write the code

[YX@VM-16-5-centos lesson7]$ touch hello.c    //创建hello.c文件
[YX@VM-16-5-centos lesson7]$ vim hello.c      //使用vim编译器编译代码

insert image description here
We can directly use gcc to generate the executable file of the test file and
insert image description here
then ./a.outexecute the executable file to get the result of our code,
insert image description here
but when we want to get the files generated by it in the four different stages of the translation environment, we How to implement it?

1. Preprocessing (macro replacement)

  • Preprocessing functions mainly include macro replacement, file inclusion, conditional compilation, and comment removal.
  • Preprocessing directives are lines of code beginning with #.
  • Example: gcc -E hello.c(This instruction is to directly obtain the preprocessed content and print it on the monitor, which is inconvenient to view and will not be demonstrated)
  • Example: gcc -E hello.c -o hello.i(Put the preprocessed content into the .i file)
  • Option "-E", the function of this option is to stop the compilation process after gccthe preprocessing is completed.
  • The option "-o" means to put the result of the previous execution into the following file, and the ".i" file is the C original program that has been preprocessed.
  • Note: The file name can be chosen at will, this does not affect it, but it is best to follow the rules and write it as an .i file, the same is true for the following files

According to the above instructions, we execute and generate preprocessed files
insert image description here
Note: .i files are not executable files, we cannot use them to ./hello.iexecute them, but they can be viewed through the vim editor

Finally you can see the contents of the file

insert image description here
We already know how to view the files generated by preprocessing, but the above case does not fully demonstrate the effect of preprocessing. Here we modify the test file first, and then generate the .i file to view the results, and we can clearly feel the preprocessing The role of the following:

insert image description here

  • For the .i file, we can see that it has 860 lines, while the .c file has only 24 lines. The redundant code in the .i file is the code included in the header file

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 of 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.c(directly generate the compiled content from the .c file, display it on the monitor, and undergo preprocessing again, it is not recommended to use this way)
  • Example: gcc -S hello.i -o hello.s(This instruction is to generate .s file from .i file)
  • Example: gcc -S hello.c -o hello.s(It is also possible to directly generate a .s file from a .c file and undergo preprocessing again)
  • ".s" files are compiled C original programs

Here we directly generate the .i file into the .s file

insert image description here

After opening, you can clearly see the assembly code it generates

insert image description here

3. Assembly (generating machine-readable code)

  • The assembly stage is to convert the ".s" file generated in the compilation stage into an object file.
  • Here you can use the option "-c" to see that the assembly code has been converted into the binary object code of ".o"
  • Example: gcc -c hello.c(same as above, directly generate compiled data and display it on the monitor)
  • Example: gcc -c hello.c -o hello.o(same as above, starting from the .c file and re-processing, compiling and assembling)
  • Example: gcc -c hello.s -o hello.o(from the compiled file to generate the assembled file)

Here we will generate the compiled .o file from the .s file

insert image description here
After opening, what we see is that the machine can read other codes, which we cannot understand

insert image description here

4. Link (generate executable file or library file)

  • After successful compilation, it enters the linking phase.
  • Example: gcc hello.c -o hello(From the .c file through the translation environment until the link)
  • Example: gcc hello.o -o hello(The .o file generated by assembly generates a linked file)
  • The linked file is an executable file, which can be executed directly to ./helloget the program result

We use .o files to generate linked files

insert image description here

View the contents of the executable

insert image description here
machine-readable code

question

Since the contents of the files generated after assembly and linking are machine-recognizable codes, can the files generated after assembly be executed?

Let's perform operations on it first:
insert image description here
find that the file does not have executable permissions, add it, and execute:

insert image description here

Conclusion: Even if a file has an executable program, if it is not an executable file, there is no way to execute it, and it proves that .othe file cannot be executed

Use to chmod -x hello.orevoke its executable permissions.

3. Function library

In the linking phase, we linked the library file, so what is a library file? This will be explained in detail.

The reason why the C/C++ program we wrote can run on the compiler is because our compiler converts the program into an executable program through the translation environment, and finally outputs the result.

In addition to the code we write, the programs we write also call the functions provided in the header files. For example, the functions we often use are declared printf、scanfin the header files stdio.h. These header files are stored in /usr/includedirectories and subdirectories under Linux. :

insert image description here
Since the declaration of these functions is placed in the header file, where are they defined?

We know that the translation environment is divided into four steps, the function of the last step 链接is to combine the files generated after assembly with the library files, and the functions declared in the header files are stored in the library files.

Under the Linux system, all these function implementations will be implemented in libc.so.6the named library file. If there is no special instruction, gcc will search in the default search path of the system “/usr/lib”, that is, link to the libc.so.6library function. This enables the function "printf" or whatever, and that's what linking does.

insert image description here
For example, let's take a look at the executable file a.out of the test file hello.c written above, and look at the library file it links to. Here we need to use lddinstructions to view it.

insert image description here
In fact, when we install compilers such as vs2019 and vs2022, the most important task is to help us download and install the language header files and library files.

So we can conclude that the function library itself is a file, which is used to define and store functions.

1. Classification of function libraries

There are two types of function libraries, static libraries and dynamic libraries. Their differences are as follows:

(1) Dynamic library

name:libXXXXX.so

Among them, XXXXX can be any name, such as the library file used above libc.so.6, it is a dynamic library. Remove the prefix lib and the suffix .so cto show that it is a library written in c language, and the remaining .6 represents the version number.

The role of the dynamic library: the dynamic link library file, in the linking stage, copy the address of the code we need in the dynamic library to the relevant location in our own executable program, and call the function in the dynamic library through the address when running the program to ensure that the code of normal operation.

When the code we write is linked, if it is not specified, the default link is the dynamic library.

(2) Static library

name:libXXXXX.a

As above, lib is the prefix, .a is the suffix, and any name can be in the middle

The role of the static library: the static link library file, when compiling and linking, copy the code we need in the static library to the executable file, so the generated file is relatively large, but the library file is no longer needed at runtime.

For example: when we write a program, the printffunction may appear more than once, then each printffunction will be replaced with the corresponding code in the static library, which will make the file larger, and the printffunctions replaced many times waste space.

Static links need to be linked by ourselves. The linking method is as follows:

  1. We can use the filecommand to check whether the executable file is linked to a dynamic library or a static library, or use the lddcommand to check the name of the link library, and judge according to the name
    insert image description here

  2. gcc hello.c -o hello-static -staticMake the resulting hello-staticexecutable statically linked via a directiveinsert image description here

    • It is normal for the files after static link and dynamic link to differ by about 10 times
  3. I am using a cloud server. By default, there are only dynamic libraries. We need to manually install static libraries to use them. The method is as follows:

     sudo yum install -y glibc-static   //在普通用户下安装c语言静态库
    yum install -y glibc-static        //在root用户下安装c语言静态库
    
    sudo yum install -y glibc-static libstdc++-static    //在普通用户下安装c++静态库
    yum install -y glibc-static libstdc++-static    //在root用户下安装c++静态库
    

2. Difference

The dynamic library calls the corresponding function in the dynamic library through the address corresponding to the dynamic library code in the program to complete the execution of the code. If the dynamic library suddenly disappears, the executable program will not be able to execute.

The static library finds the corresponding function in the library through the address generated by the function name, copies it to the executable file, and calls it in the file, which makes the executable file execute code even without a static library, but with the copy, Executables also get larger.

3. Expansion

The instructions we use in Linux are all written in C language. We can check the link libraries of different instructions to see if they are written in C language, as follows:

View the library file of the ls command:

insert image description here
View the library file of the which command:

insert image description here

So instructions are programs.

  • This is the only knowledge about the function library in this blog, and more content will be explained in Basic I/O.

4. Memory

1. Options

In the four steps of the translation environment, we used -ooptions, and only used links . We can remember the -oremaining three steps through the button on the upper left corner of the keyboard .Esc

Preprocessing—— -E, compilation—— -S, compilation——c

If we forget, we only need to look at the Escbutton in the upper left corner of the keyboard, but note that Eand Sare capitalized.

2. Suffix

The file generated by the link has no suffix and does not need to be memorized, and the other three correspond to the suffix of the image file in order.iso

Five.gcc options

Among the options of gcc, the most commonly used options are the options of the above four steps of the translation environment, which are summarized and put here together with other options for your convenience.

  • -E only activates preprocessing, this does not produce a file, it needs to be redirected 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 statically links the generated files.
  • -g Generate debugging information. The GNU debugger can take advantage of this message.
  • -shared This option will try to use the dynamic library, so the generated file is relatively small, but the system needs the dynamic library.
  • -O 0
  • -O 1
  • -The 2
  • -O 3 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.

Guess you like

Origin blog.csdn.net/m0_52094687/article/details/128608256