Linux | Use of gcc/g++

Table of contents

Preface

1. Program translation process

1. Precompilation

2. Compile

3. Compilation

4. Link

(1) What does the link do?

(2) Dynamic link

(3) Static link

(4) How to use gcc for dynamic linking and static linking

at last


Preface

        This chapter mainly takes everyone to learn the tools for compiling C/C++ under Linux, as well as some basic knowledge about dynamic and static libraries;

1. Program translation process

        The programs written in our C language and C++ are translation programs. Here we take C language as an example to analyze the program translation process;

1. Precompilation

        When we finish writing a C language program, first of all, we need to pre-compile; pre-compilation mainly expands the header file, Macro replacement, Conditional compilation and Uncomment; We can use the following command to generate our precompiled file (file suffix ending with .i);

gcc -o generated file name.i -E precompiled file name.c

        The picture above shows the source program we wrote. Let’s verify whether our pre-compilation phase has completed the above work; we perform the following execution to generate the pre-compiled file test.i;

        From the size of the pre-compiled file, it can be clearly seen that it is much larger than our source file. We then use vim to view our test.i file, as shown below;

        We found that our file suddenly became more than 800 lines. The previous addition was what our stdio file looked like after it was expanded. Therefore, our test.i file became much larger because our library file code was copied to the pre- The compiled file is in, and it is not difficult to find that our previous comments have been removed, our macros have been replaced, and conditional compilation has been performed, which is completely consistent with our expectations;

2. Compile

        This step mainly implements compiling our C language code into assembly code. The assembly code file we generate has s as the suffix name. The specific instructions are as follows;

gcc -o The file name after compilation is .s -S .c file or .i file can be

        Friends who have studied assembly language should be very familiar with it. This is our assembly code, and the compilation phase completed its work as we expected;

3. Compilation

        This step is mainly to convert our assembly code into machine instructions. The generated file is called Redirectable binary target file (target file), this file generally has the suffix o under Linux, and the suffix obj under Windows. The specific instructions are as follows;

gcc -o generates file name -c .c file or .i file or .s file can be 

        We can use the od command to read our redirectable target file, as shown below;

        The instructions generated here are already machine instructions, but they still cannot be executed directly and must be linked before execution;

4. Link

(1) What does the link do?

        In fact, our above header files, such as stdio.h, only have function declarations and no function definitions. Therefore, when we use our library functions printf and scanf functions, it is not enough to have function declarations. We must There are also specific function definitions. These function definitions are placed in dynamic libraries or static libraries, so our links also include dynamic links and static links; we first complete the link, and the specific instructions are as follows;

gcc -o generates an executable program name. .c file/.i file/.s file/.o file can be

        We can check the name of the dynamic library/static library used by the file through the ldd command or the file command, and what linking method is used;

Supplement:Under Linux, the suffix of dynamic library files is .so, and the suffix of static library files is .a. Under Windows, the suffix of dynamic library files is . dll, the suffix of static library files is .lib;

(2) Dynamic link

        In dynamic linking, once our program finds code that needs to use a library function, it will save the address of the library function in the dynamic library. When running to this part, we jump directly to the code of the dynamic library through the address. Execute the function, and after executing the function, continue to return to the program to execute the subsequent code of the program; therefore, once there is a problem with our dynamic library, our program cannot be executed correctly;

Summarize:

Advantages of dynamic linking: the executable program is smaller because only the library function address is saved;

Disadvantages of dynamic linking: It relies heavily on dynamic library files. If there is a problem with the file, the program may have problems;

(3) Static link

        In static linking, our program will directly copy the definition of the library function that needs to be used into our executable program. When we run the executable program, we do not need a static library. We can directly adjust the code we copied. ;

Summarize:

Advantages of static linking: no dependence on static library files. Once an executable program is generated, it can still run even if the static library is deleted;

Disadvantages of static linking: The executable program generated by static linking will copy the function definitions in the static library, so it will become very large;

(4) How to use gcc for dynamic linking and static linking

        In gcc, we use dynamic linking by default, so we can directly use the normal method to generate an executable program, for example;

gcc -o test-d test.c

        ​ ​ ​ And if we want to use static linking to connect, we can add a static option, as shown below;

gcc -o test-s test.c -static

Supplement: Some friends may not have the static library installed. You can install the static library through the following command.

sudo yum install -y glibc-static (Centos)

        Obviously, the executable program we generate using static linking is obviously about ten times larger than the dynamically generated executable program. This is also consistent with our previous reasoning. We use the ldd and file instructions to check the libraries that the executable program we generated depends on. ;

        test-s is an executable program generated by static linking, so it has no dependent dynamic libraries, while test-d is an executable program generated by dynamic linking, so it has dependent dynamic libraries. We can see these two executables through file Whether the program is dynamically linked or statically linked;

at last

        The usage of our g++ is almost the same as that of our gcc. We can completely use g++ in the same way as gcc, but we will not demonstrate it here;

Guess you like

Origin blog.csdn.net/Nice_W/article/details/133912080