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

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

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

1.静态链接库

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

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

static.h文件:

  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> 
#ifndef LIB_H
#define LIB_H

extern "C" int sum(int a,int b);

#endif

static.cpp文件:

  1. <span style="font-size: 14px;">#include"static.h" 
  2.  
  3. int sum(int a,int b) 
  4.     return a+b; 
  5. }</span> 
#include "static.h"

int sum(int a,int b)
{
	return a+b;
}
编译这个项目之后,会在debug文件夹下生成static.lib文件,这个就是我们需要的静态链接库。


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

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

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

  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> 
#include <stdio.h>
#include <stdlib.h>
#include "static.h"

#pragma comment(lib,"static.lib")

int main()
{
	printf("%d\n",sum(1,2));
	system("pause");
	return 0;
}


编译运行可得结果:3

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


2.动态链接库

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

dynamic.h文件:

  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> 
#ifndef DYNAMIC
#define DYNAMIC

extern "C" __declspec(dllexport)int sum(int a, int b);

#endif DYNAMIC

dynamic.cpp文件:

  1. <span style="font-size: 14px;">#include"dynamic.h" 
  2.  
  3. int sum(int a,int b) 
  4.     return a+b; 
  5. }</span> 
#include "dynamic.h"

int sum(int a, int b)
{
	return a+b;
}
编译这个项目,会在debug文件夹下生成dynamic.dll文件。

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

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

  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> 
#include <stdio.h>
#include <stdlib.h>
#include<Windows.h>
#include "dynamic.h"
int main()
{
	HINSTANCE hDll=NULL;
	typedef int(*PSUM)(int a,int b);
	PSUM pSum;
	hDll = LoadLibrary(L"dynamic.dll");
	pSum = (PSUM)GetProcAddress(hDll,"sum");
	printf("%d\n",pSum(1,2));
	system("pause");
	FreeLibrary(hDll);
	return 0;
}

编译运行结果为:3


特别提示:

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值