Static library and dynamic library programming technology

(1) Library

1. What is a library

    Libraries are essentially a way of code reuse, that is, a binary format of precompiled executable code,

It can be loaded into memory and executed, such as the C runtime library, which implements basic functions. We don't need to write it again, just call the interface directly.

Libraries are divided into static libraries and dynamic libraries.



Second, the difference between static library and dynamic library

1. Static function library

The name of this type of library is generally xxx.lib, and the statically compiled file is relatively large, because all the data of this function library

will be integrated into the object code

Advantages: That is, the compiled execution program does not need external function library support, because all the functions used are compiled into it, 

Disadvantage: If something changes in the static library, your program must also be recompiled.


2. Dynamic function library

The library name is generally xxx.dll (xx.lib can also be included for link processing at compile time, or not included,

direct dynamic call) 

The dynamic function library will not be compiled into the object code, and the library function will only be called when your program executes the relevant code.

Advantages: Therefore, the executable file generated by the dynamic function library is smaller, because it is not integrated into your program

Dynamic library changes will not affect your program. The dynamic function library is easy to upgrade.

Disadvantages: The program must provide the corresponding library in the running environment,





Three, static library example

1. Select the Win32 project

blob.png


2. Precompile the header, automatically generate the stdafx.h header file, the function is to obtain faster compilation speed

blob.png


3. Add a class

blob.png


4. Add the addition interface, the same as c++, no explanation. Note that the implementation of the function should be placed in the cpp file,

Although there is no error, but it violates the original intention of the static library, 



5. Implement a separate interface without relying on classes, such as implementing a subtraction interface

Declare  extern "c" like this to let the compiler compile this part but in c form

in the header file

extern "C" 

{

int sub(int a,int b);

int Doubles(int a);

}


6. Then compile and generate

blob.png


7. Import and use, the interface is declared in the .h file, so you need .h to find the function,

import lib using code

#pragmacomment(lib,"lib.lib")

blob.png


If you want convenience, you can put the project directory directly

blob.png


If your lib file is somewhere else, you can say set additional library directory

blob.png


如果你不想在代码导入lib 可以在这里导入

blob.png



8.创建对象调用接口

	CTestLib t;
	//调用类接口
	int a = t.Add(10,10);

	//调用函数
	int subs = sub(30,10);
	int dbInter = Doubles(30);







四、动态库实例


1.创建Win32项目,DLL即可


blob.png



2.DllMain函数

blob.png

作用: 动态库dll和静态库区别是: 动态库是可以独立运行的文件,

通俗说他和可执行文件没有多大区别

当其他可执行程序(exe或者其他dll)调用该dll时候,系统会执行一个入口函数.

做一些初始化之类的工作,当然这个入口函数和可执行文件exe有一个最大的区别

就是这个入口函数  不是必须的, 也就是说没有这个函数依然能编译dll


参数二表明了系统调用DLL的原因:

DLL_PROCESS_ATTACH  进程加载

DLL_PROCESS_DETACH  进程卸载

DLL_THREAD_ATTACH   线程加载

DLL_THREAD_DETACH   线程卸载

通过这四种情况分析系统何时调用轮到DllMain



3.DLL_PROCESS_ATTACH 

一个程序要调用DLL里的函数,首先要把DLL文件映射到进程的地址空间,

要把一个DLL文件映射到进程的地址空间,两种方法:

静态链接和动态链接的LoadLibrary或者LoadLibarayEx


当一个DLL文件被映射到进程的地址控件时,系统调用该DLL的DllMain函数,

传递fdwReason参数为DLL_PROCESS_ATTACH,这种调用只发生在第一次映射

如果同一个进程来为已经映射进来的DLL再次调用,系统只会增加DLL使用次数,


4.DLL_PROCESS_DETACH  

DLL被从进程地址空间解除,DLL处理该值时,应处理相关清理工作


什么时候DLL被从进程的地址空间解除映射呢?有两种情况

1_) FreeLibrary解除DLL映射(有几个LoadLibrary,就要有几个FreeLibrary)

2_) 进程结束而解除DLL映射,在进程结束前还没有解除DLL时,进程结束会解除

DLL映射,如果是调用 TerminateProcess终止指定进程和其线程

系统就不会调用DLL_PROCESS_DETACH  



5.DLL_THREAD_ATTACH   

当进程创建线程时,系统查看当前映射到进程地址空间中的所有DLL文件映射,

DLL_THREAD_ATTACH会触发


新创建的线程负责执行DLL的DllMain函数, 当所有的DLL都处理完后,系统才运行

进程执行其他的线程函数


每次新建线程都会调用, 线程中建立线程也会调用。



6.DLL_THREAD_DETACH   

线程调用ExitThread结束线程,(线程函数返回时, 系统也会自动调用ExitThread)

系统查看当前进程空间中所有的DLL映射文件, 并DLL_THREAD_DETACH调用DllMain函数

通知所有DLL执行线程清理工作





五、动态库导入导出

1.打开C++ 预处理器

区分动态库是导入的还是导出的

blob.png


2.有一个自定义的预定义,这个预定义随便是什么,因为这个

只在你这个工程有效,其他的无效



3.如果有这个定义说明是导出

如果没有这个定义说明是导入

现在这个宏定义是定义的,所以他是导出.

blob.png



4.类名前面添加即可

blob.png



5.添加类函数

声明 不需要调用形式,  给类添加即可

blob.png

实现

blob.png


C语言形式 全局接口 声明前加上调用形式

blob.png




6.编译生成

blob.png




7.导入和使用动态库,所以操作和静态库一样,就是多个个DLL文件

这个文件和exe放一个目录,会静态链接



8. There is also a linking method for dynamic libraries, which dynamically loads dll: LoadLibrary

The so-called dynamic loading means that the dll does not have to be loaded at the beginning when the program is running.

When the program needs it, it is dynamically added, 

In this way, we no longer use lib for static linking, or even the h header file,

Use GetProcAddress directly, but in this way, it is best to only use extern "C"

interface at the beginning, 



9. Download the Depends decompilation tool to view the contents of the dll

The above 3 are class style

Sub function in global form

blob.png



10. Use dynamic function link library through dynamic linking

To call a function dynamically, you need to know the function prototype.

	HMODULE hdl = ::LoadLibrary(L"TestDll.dll");
	
	if(!hdl)
		printf("load error");

	 //Get the function pointer of the interface. The second parameter is the function name
	FUNC_SUB fnuc = (FUNC_SUB)::GetProcAddress(hdl,"Sub");
	if(fnuc != NULL)
	{
		printf("110 minus 20=%d",fnuc(110,20));
	}
	//unmap
	::FreeLibrary(hdl);













Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324738189&siteId=291194637