The creation and use of dynamic library under Windows

To solve the two problems of space waste and difficulty in updating, the easiest way is to separate the modules of the program to form independent files, rather than link them statically. To put it simply, the target program that composes the program is not linked, and the link is performed when the program is running, that is, the entire linking process is postponed to run time. This is the basic idea of ​​dynamic linking.

How to create it?

1. Create a new project mydll

2. Add mydll.h file and mydll.c

Add the following content:

#include<stdio.h>

//导入函数  智能在当前项目中使用
//int mySub(int a,int b);

//导出函数  能在项目外使用  __declspec特殊声明
__declspec (dllexport) int mySub(int a,int b);

 

How to configure the static library?

First, right-click the project-->Properties-->General-->Configuration Type-->Drop-down on the right to select Dynamic Library-->Application

 

Regenerate the solution, after generating the .lib file and .dll file

The .lib file generated by the dynamic library is different from the .lib file generated by the static library

The .lib file in the dynamic library will only store the declaration of some exported functions and the declaration of some variables, and the specific implementation is stored in the .dll file.

Similarly, we create a new project to test it.

Copy the .lib file, .dll file and mydll.h to the project

Add those two files to the project, this time you can use #pragma comment(lib,"./mydll.lib") (just add the path of the .lib file later, it will automatically find the .dll)

#include<stdio.h>
#include"mydll.h"

#pragma comment(lib,"./mydll.lib")

int main()
{
	
	int ret = mySub(20,10);
	printf("ret = %d\n",ret);
	return 0;
}

 

 

 

Guess you like

Origin blog.csdn.net/weixin_42596333/article/details/104589061