VS2010下创建静态链接库和动态链接库

来源:互联网 发布:key tronic 淘宝 编辑:程序博客网 时间:2024/05/18 12:30

转自:http://blog.csdn.net/love_cppandc/article/details/8502773

下面面介绍一下用VS2010如何创建静态链接库和动态链接库,并测试创建的库。

1.静态链接库

打开VS2010,新建一个项目,选择win32项目,点击确定,选择静态库这个选项,预编译头文件可选可不选。

在这个空项目中,添加一个.h文件和一个.cpp文件。名字我们起为static.h和static.cpp

static.h文件:

[cpp] view plaincopyprint?
  1. <span style="font-size: 14px;">#ifndef LIB_H 
  2. #define LIB_H 
  3.  
  4. extern "C"int sum(int a,int b); 
  5.  
  6. #endif</span> 
[cpp] view plaincopyprint?
  1. <span style="font-size: 14px;">#ifndef LIB_H  
  2. #define LIB_H  
  3.   
  4. extern "C" int sum(int a,int b);  
  5.   
  6. #endif</span>  

static.cpp文件:

[cpp] view plaincopyprint?
  1. <span style="font-size: 14px;">#include"static.h" 
  2.  
  3. int sum(int a,int b) 
  4.     return a+b; 
  5. }</span> 
[cpp] view plaincopyprint?
  1. <span style="font-size: 14px;">#include "static.h"  
  2.   
  3. int sum(int a,int b)  
  4. {  
  5.     return a+b;  
  6. }</span>  
编译这个项目之后,会在debug文件夹下生成static.lib文件,这个就是我们需要的静态链接库。


下面说明如何调用静态链接库。

首先需要新建一个空项目,起名为test。将之前static项目下的static.h和static.lib这个2个文件复制到test项目的目录下,并在工程中加入static.h文件。

新建一个test.cpp文件如下:

[cpp] view plaincopyprint?
  1. <span style="font-size: 14px;">#include <stdio.h> 
  2. #include <stdlib.h> 
  3. #include "static.h" 
  4.  
  5. #pragma comment(lib,"static.lib") 
  6.  
  7. int main() 
  8.     printf("%d\n",sum(1,2)); 
  9.     system("pause"); 
  10.     return 0; 
  11. }</span> 
[cpp] view plaincopyprint?
  1. <span style="font-size: 14px;">#include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include "static.h"  
  4.   
  5. #pragma comment(lib,"static.lib")  
  6.   
  7. int main()  
  8. {  
  9.     printf("%d\n",sum(1,2));  
  10.     system("pause");  
  11.     return 0;  
  12. }</span>  


编译运行可得结果:3

#pragma comment(lib,"static.lib"),这一句是显示的导入静态链接库。除此之外,还有其他的方法,比如通过设置路径等等,这里不做介绍。


2.动态链接库

和创建静态链接库一样,需要创建一个空的win32项目,选择dll选项。创建dynamic.cpp和dynamic.h文件

dynamic.h文件:

[cpp] view plaincopyprint?
  1. <span style="font-size: 14px;">#ifndef DYNAMIC 
  2. #define DYNAMIC 
  3.  
  4. extern "C"__declspec(dllexport)int sum(int a,int b); 
  5.  
  6. #endif DYNAMIC</span> 
[cpp] view plaincopyprint?
  1. <span style="font-size: 14px;">#ifndef DYNAMIC  
  2. #define DYNAMIC  
  3.   
  4. extern "C" __declspec(dllexport)int sum(int a, int b);  
  5.   
  6. #endif DYNAMIC</span>  

dynamic.cpp文件:

[cpp] view plaincopyprint?
  1. <span style="font-size: 14px;">#include"dynamic.h" 
  2.  
  3. int sum(int a,int b) 
  4.     return a+b; 
  5. }</span> 
[cpp] view plaincopyprint?
  1. <span style="font-size: 14px;">#include "dynamic.h"  
  2.   
  3. int sum(int a, int b)  
  4. {  
  5.     return a+b;  
  6. }</span>  
编译这个项目,会在debug文件夹下生成dynamic.dll文件。

下面介绍如何调用动态链接库,这里讲的是显示的调用。

在刚才的test项目下,把static.lib和static.h文件删除,把dynamic.h和dynamic.dll复制到该目录下,并在项目中添加dynamic.h文件,修改test.cpp文件为:

[cpp] view plaincopyprint?
  1. <span style="font-size: 14px;">#include <stdio.h> 
  2. #include <stdlib.h> 
  3. #include<Windows.h> 
  4. #include "dynamic.h" 
  5. int main() 
  6.     HINSTANCE hDll=NULL; 
  7.     typedef int(*PSUM)(int a,int b); 
  8.     PSUM pSum; 
  9.     hDll = LoadLibrary(L"dynamic.dll"); 
  10.     pSum = (PSUM)GetProcAddress(hDll,"sum"); 
  11.     printf("%d\n",pSum(1,2)); 
  12.     system("pause"); 
  13.     FreeLibrary(hDll); 
  14.     return 0; 
  15. </span> 
[cpp] view plaincopyprint?
  1. <span style="font-size: 14px;">#include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include<Windows.h>  
  4. #include "dynamic.h"  
  5. int main()  
  6. {  
  7.     HINSTANCE hDll=NULL;  
  8.     typedef int(*PSUM)(int a,int b);  
  9.     PSUM pSum;  
  10.     hDll = LoadLibrary(L"dynamic.dll");  
  11.     pSum = (PSUM)GetProcAddress(hDll,"sum");  
  12.     printf("%d\n",pSum(1,2));  
  13.     system("pause");  
  14.     FreeLibrary(hDll);  
  15.     return 0;  
  16. }  
  17. </span>  

编译运行结果为:3


特别提示:

1.extern "C"中的C是大写,不是小写

2.如果从VS2010中直接运行程序,lib和dll需要放到test项目的目录下;如果想双击项目test下的debug文件中的exe文件直接运行的话,需把lib和dll放入debug文件夹下。