Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use static links and dynamic links in C++

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to use static links and dynamic links in C++, I believe most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article. Let's learn about it!

I. GCC work flow

Pre-processing: expand the # header file, replace it with macros, and remove comments (generate .i file)

Compilation: generate assembly files (.s files) from preprocessed files, mainly to check syntax and semantic problems

Assembler: generate an assembly file into an object file (.o file)

Link: combine the corresponding code in the function library into the object file to generate the executable file (default a.out file)

The o file does not execute immediately because it may occur: a function in one .cpp file references a symbol defined in another .cpp file / calls a function in a library file. The purpose of linking is to link the target files corresponding to these files into a whole to generate executable files.

Static link and dynamic link

Library: a file that contains data and executed code that cannot be executed alone and can perform certain functions as part of a library.

The existence of the library can make the program modular, speed up the recompilation of the program, realize code reuse, and facilitate updating the program.

The program library is divided into static link library and dynamic link library.

1. Static link

In the linking phase, the .o file generated by assembly and the required libraries are linked and packaged into an executable file, and no other library files are called when the program is running.

A static library, which can be seen as a collection of target code, has been added to the executable code before the executable program runs and becomes part of the executable program.

The advantages of static links: less dependence on the running environment and good compatibility.

Disadvantages of static linking: the generated program is larger, requires more system resources (all required libraries are packaged into executables), and takes more time to load into memory

Once the library function is updated, the application must be recompiled

Here, we make a static library to add, subtract, multiply and divide. First, write add.c, sub.c, mul.c, div.c files and corresponding .h files, and then write text.c files for testing. Gcc-c generates the object file .o, and then packages the .o file to create a static library libtext.a

2. Dynamic link

Problems with static libraries:

(1) if both .o files use the same static library, two copies of the static library's code will be copied in memory and then packaged into executable files together with two .o files, resulting in a waste of space.

For example: a static library occupies 1m of memory, there are 2000 .o files using this static library, and there are 2000 static library code in memory (nearly 2000GB), which is a serious waste of space.

(2) the required library is copied to the executable, and if a library is updated, all executables associated with it need to be recompiled.

In order to solve these two problems, it leads to the dynamic library (also known as shared library). When the program is compiled, the dynamic library is not linked to the object code, but is loaded at run time. If different applications call the same library, there is only one instance of the shared library in memory, which avoids waste. Because the dynamic library is loaded at run time, and the horse dung caused by the update, deployment and release of the static library is solved, the user only needs to update the dynamic library.

Advantages of dynamic linking: when linking, only establish a relationship with the required library functions

Transfer the required resources into the executable program only when the program is running

Simplify the upgrade of the program and have a smaller program volume

To achieve resource sharing between processes, there is only one instance of the dynamic library in memory to avoid full copy

Disadvantages of dynamic linking: it depends on dynamic libraries and cannot be run independently

The problem of dependent version of dynamic library is serious.

Similarly, we make dynamic libraries that add, subtract, multiply and divide.

We copy the dynamic library .so and the test file .c to the current directory, so that when the system loads the executable file, it can know the name of the dependent library, but we also need to find the absolute path of the dynamic library, which requires the system dynamic loader (dynamic linker/loader). For executable programs in elf format, it is done by ld-linux.so*. Search the DT_PATH segment (environment variable) LD_LIBRARY_PATH,/etc/ld.so.cache file list of the elf file, and the / urs/lib directory finds the library file and loads it into memory.

Solution to failed loading of dynamic libraries: here are two solutions to find dynamic libraries

The above is all the contents of this article entitled "how to use static links and dynamic links in C++". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report