C++ library files, and the characteristics of dynamic library files

C++ library file

Windows static library: .lib

Contains two files testlib.lib and testlib.h. When writing a program to call the functions contained in the static library, the implementation of the function has been put into the generated exe file after compiling, so the later operation of the exe does not need to rely on Static library, the cost is that the size of the exe will become larger. At present, mainstream programs generally do not use static libraries.
insert image description here

Windows dynamic library: .dll

Contains two files, testlib.lib (required in the compilation stage) and testlib.dll (used in the calling and running stage). When writing a program to call the functions contained in the dynamic library, the compilation stage only puts the relocation information of the function into the exe file In, so the later operation of the exe needs to rely on the dynamic library. It is worth mentioning that although both dynamic and static libraries have a .lib file, they are completely different. The .lib file of the dynamic library is only needed in the compilation phase of your call, and the .dll is only needed in the runtime phase of the call.
insert image description here

Linux dynamic library: libXXX.so

Like the dynamic library of Windows, the name is as above.

Linux static library: LibXXX.a

compile library

Windows static library

├─static_lib.cpp
├─static_lib.h
├─pch.cpp #These two are precompiled headers generated by vc++ itself, here we don't pay attention to
└─pch.h #These two are precompiled headers generated by vc++ itself Head, we don't pay attention here

#include "pch.h"
#include "static_lib.h"
int add(int x, int y)
{
    
    
	return x + y;
}

#pragma once
int add(int x, int y);

After the compilation is completed, get the static_lib.lib file in the DEBUG folder, and add a static_lib.h just used. With these two files, you can call the function add, and the calling process will follow.

Windows dynamic library

├─dynamic_lib.cpp
├─dynamic_lib.h
├─pch.cpp #These two are precompiled headers generated by vc++ itself, we don't pay attention here└─pch.h
#These two are precompiled headers generated by vc++ itself Head, we don't pay attention here

#include "pch.h"
#include "dynamic_lib.h"
#define  DLL_API _declspec(dllexport)
DLL_API int add(int a, int b)   //实现两个整数相加
{
    
    
	return a + b;
}

#pragma once
#define  DLL_API _declspec(dllexport)
int add(int x, int y);

After compiling, two files dynamic_lib.dll and dynamic_lib.lib will be generated in DEBUG

Linux static library

├─static_lib.c
└─static_lib.h

#include "static_lib.h"
int add(int a, int b)
{
    
    
	int c;
	c = a + b;
	return c ;
}

#pragma onece
int add(int a, int b);

The compilation process is divided into two steps, first generate .o files, and then archive them as .a static library files according to the .o files
1, gcc -c static_lib.c
and then archive them as static library files, thus generating the static library libstatic_lib.a
2, ar crv libstatic_lib.a static_lib.o

Linux dynamic library

├─dynamic_lib.c
└─dynamic_lib.h

#include "dynamic_lib.h"
int add(int a, int b)
{
    
    
	int c;
	c = a + b;
	return c ;
}
#pragma onece
int add(int a, int b);


Compile and generate the .so file, enter ~$ gcc dynamic_lib.c -shared -fPIC -o libdynamic_lib.so in the command line
. Note that the name of the generally generated .so file should start with lib

call library

Call Windows static library files

├─lib_test.cpp
└─stdafx.h #This is the precompiled header generated by vc++ itself, we don't pay attention here Here you
need to set up the compiler, tell the compiler what the name of the static library is, and which folder it is in. The place that needs to be set in VS is the project-properties-VC++ directory, fill the path C:\Users\john\Desktop\static_lib where static_lib.h is located in the include directory, and fill the path where the previously generated static_lib.lib is located C:\Users \john\Desktop\static_lib\DEBUG fill in the library directory. Then in the project-properties-linker-input, enter static_lib.lib into the additional dependencies (this step is actually equivalent to the sentence #pragma comment(lib, "static.lib") in lib_test.cpp, two You can choose one of the two). Finally compiled successfully.

Call Windows dynamic library files

├─lib_test.cpp
└─stdafx.h #This is the precompiled header generated by vc++ itself, we don't pay attention here

#include "stdafx.h"
#pragma comment(lib, "dynamic_lib.lib")
extern int add(int a, int b);
int _tmain(int argc, _TCHAR* argv[])
{
    
    
	int a = 0, b = 2, c;
	c = add(a, b);
	return 0;
}

Note that there is one less include in the code here, and one more extern. The place that needs to be set in VS is the project-properties-VC++ directory, and fill in the previously generated dynamic_lib.lib path C:\Users\john\Desktop\dynamic_lib\DEBUG into the library directory. Then in the project-properties-linker-input, enter dynamic_lib.lib into the additional dependencies (this step is actually equivalent to the sentence #pragma comment(lib, "static.lib") in lib_test.cpp, two You can choose one of the two). Finally compiled successfully. When running, you need to put the .dll file in the folder where the exe is generated, otherwise an error will occur when the compilation is successful

Dynamic library features:

Ꝏ动态库把对一些库函数的链接载入推迟到程序运行的时期。
Ꝏ可以实现进程之间的资源共享。(因此动态库也称为共享库)
Ꝏ将一些程序升级变得简单。
Ꝏ甚至可以真正做到链接载入完全由程序员在程序代码中控制(显示调用)。

Window 与 Linux 执行文件格式不同,在创建动态库的时候有一些差异。
Ꝏ在 Windows 系统下的执行文件格式是 PE 格式,动态库需要一个 DllMain 函数做出初始化的入口,通常在导出函数的声明时需要有 _declspec(dllexport)关 键字。
ꝎLinux 下 gcc 编译的执行文件默认是 ELF 格式,不需要初始化入口,亦不需要函数做特别的声明,编写比较方便。

Guess you like

Origin blog.csdn.net/weixin_43925768/article/details/128114611