当前位置:网站首页>Use of static library and dynamic library

Use of static library and dynamic library

2022-06-26 02:03:00 _ Bruce

First, let's introduce the static library ( Static link library )、 Dynamic library ( Dynamic link library ) The concept of , First of all, both are ways of code sharing .

Static library : In the link step , The connector will get the required code from the library file , Copy to the generated executable file , This library is called a static library , Its characteristic is that the executable file contains a complete copy of the library code ; The disadvantage is that if it is used many times, there will be multiple redundant copies . That is, all the instructions in the static library are directly included in the final generated EXE It's in the document . stay vs Create a new project to generate a static library in , After successful compilation and generation , Only one .lib file

Dynamic library : Dynamic link library is a library that contains code and data that can be used by multiple programs at the same time ,DLL It's not an executable file . Dynamic linking provides a way , Enables a process to call functions that are not part of its executable code . The executable code of the function is in a DLL in , The DLL Contains one or more compiled 、 Functions linked and stored separately from the processes that use them . stay vs Create a new project to generate dynamic library in , After successful compilation , Produce a .lib Documents and a .dll file , Creating a dynamic library requires

__declspec(dllexport) As a prefix to a function :__declspec(dllexport) void test1();

Then the static library and the dynamic library lib What's the difference ?

In a static library lib: The LIB Contains the function code itself ( That is, including the index of the function , It also includes the implementation of ), Add code directly to the program at compile time

In dynamic library lib: The LIB Contains the... Where the function is located DLL File and function location information in the file ( Indexes ), The function implementation code is loaded in the process space by the runtime DLL Provide

All in all ,lib It's used when compiling ,dll It's used at runtime . If you want to compile the source code , It only needs lib; If you want to make the dynamic link program run , It only needs dll.

 

Call dynamic library : Implicit linking

Implicit linking need .h file ,dll file ,lib file

(1) take dll Put it into the working directory of the project

(2) Set item properties --vc++ Catalog -- The library directory is lib Path

(3) take lib Add to project properties -- The linker -- Input -- Additional dependency ( Or add... Directly to the source code #pragma comment(lib, “**.lib”))

(4) Add... To the source file .h The header file

Then call ordinary functions as usual 、 class 、 Variable

Use static libraries

need .h file ,lib file

(1) Set item properties --vc++ Catalog -- The library directory is lib Path

(2) take lib Add to project properties -- The linker -- Input -- Additional dependency ( Or add... Directly to the source code #pragma comment(lib, “**.lib”))

(3) Add... To the source file .h The header file

原网站

版权声明
本文为[_ Bruce]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260023565945.html