[Static library and dynamic library][VS2022]

Foreword:

When we write code, we can write it in modules, and finally we can collaborate and integrate it;
we can separate the implementation and declaration of the code.
For example: My blog C language boxing game uses modular programming.
What is a library:
a library is an existing, mature, and reusable code that is written. In reality, every program depends on many basic underlying libraries. In essence, a library is a binary form of executable code that can be loaded into memory by the operating system for execution. There are two kinds of libraries: static libraries (.a, .lib) and dynamic libraries (.so, .dll).

1. Static library

The file compiled by the static library will be relatively large. The biggest advantage of this type of function library is that the executable file compiled successfully can run independently without requiring the outside to read the contents of the function library; but from the perspective of the difficulty of upgrading Obviously there is no advantage, if the function library is updated, it needs to be recompiled.

Two, dynamic library

When the dynamic library is compiled, there is only one "pointing" position in the program, that is to say, when the executable file needs to use the mechanism of the function library, the program will read the function library to use; that is to say, it can Executable files cannot be run by themselves. In this way, it is convenient to upgrade from the perspective of product function upgrade, as long as the corresponding dynamic library is replaced, there is no need to recompile the entire executable file.

3. The use of static libraries and dynamic libraries

For example, we have written a function and want to sell it to make money. We don’t want others to see how the function I wrote is implemented. We only give a function declaration (in the header file), what should we do?
3.1 Create a project, create a file
insert image description here
3.2 Solution Explorer->project_7_12game->(right click) pop-up shortcut menu->click properties->pop-up property page window->find configuration properties click general->find configuration type, in configuration Find a down arrow on the right side of the type -> click the arrow, and click Static Library in the drop-down list.
insert image description here
insert image description here
3.3 Generate a .lib file
insert image description here

You can find it through the path in the resource manager:
insert image description here
the .lib file generated by compiling these three files is called a static library. When
we open this file, it will display garbled characters, because it is a binary file
insert image description here
3.4. We sell the file as this . If the lib file and the .h file are sold, others will not be able to see how the function is implemented.
3.5 Using .lib files and .h files
Write a program that uses .lib files and .h files.
insert image description here
Find the folder
, add the .lib file and the .h file, and open
insert image description here
the header file in the project ; at this time, we need to use the function of the game.h file to import the static library (.lib file) #pragma comment(lib,"Project_7_12game .lib")//Import static library Press ctrl+F5 to call the function in .h
insert image description here

insert image description here


insert image description here

Guess you like

Origin blog.csdn.net/plj521/article/details/131690905