(20200508)静态链接库(.lib)+动态链接库(.dll)

 参考:

【1】https://segmentfault.com/a/1190000008634703

【2】https://www.cnblogs.com/chechen/p/8676226.html

——————————————————

20200508

第一部分:

静态链接库的创建(vs2013):

【1】文件->新建项目->常规->空项目

【2】在头文件中添加mylib.h文件:(函数声明)

#pragma once

#ifdef __cplusplus
extern "C"{
#endif

	int myAdd(int a, int b);

#ifdef __cplusplus
}
#endif

 

说明:

1】mylib.h内容:

【3】在源文件中添加mylib.c文件:(函数实现)

#include "mylib.h"

int myAdd(int a, int b)
{
	return a + b;

}

【4】配置项目属性:

【5】编译生成新的解决方案,在Debug文件夹下会得到mylib.lib。

 

【6】将该lib文件和相应文件头给用户,用户就可以使用该库的函数。

 

————————

第二部分:

静态链接库的使用:

【1】方法1:

使用:#pragma comment (lib,"./staticLib.lib")

#include <stdio.h>
#include <stdlib.h>
#include "mylib.h"

#pragma comment(lib,"./staticLib.lib")
int main()
{
	int a = 10;
	int b = 20;
	int c = myAdd(a, b);
	printf("%d \n", c);

	system("pause");
	return 0;
}

 

【2】方法2:

#include <stdio.h>
#include <stdlib.h>
#include "mylib.h"

//#pragma comment(lib,"./staticLib.lib")
int main()
{
	int a = 10;
	int b = 20;
	int c = myAdd(a, b);
	printf("%d \n", c);

	system("pause");
	return 0;
}

 

  
 

第一部分:

动态链接库的创建:

【1】文件->新建项目->常规->空项目

【2】在头文件中添加mydll.h文件:(函数声明)

#pragma once

#ifdef __cplusplus
extern "C"{
#endif
	//一种是内部函数,只在dll内部使用
	int fun(int a)
	{
		return a;
	}

	//外部函数(导出函数)
	__declspec(dllexport) int myMinus(int a, int b);  //两个整数相减

#ifdef __cplusplus
}
#endif

说明:

【3】在源文件中添加mydll.c文件:(函数实现)

#include "mydll.h"

int myMinus(int a, int b)
{
	return a - b;

}

【4】配置项目属性:

【5】编译生成新的解决方案,在Debug文件夹下会得到mydll.dll。

说明:

 

【6】将该dll文件和相应文件头给用户,用户就可以使用该库的函数。

 

 

——————

第二部分:

动态链接库的使用:

【1】将mydll.h和mydll.dll和mydll.lib复制到项目目录下

【2】

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

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

int main()
{
	int a = 10;
	int b = 20;
	int c = myMinus(a, b);
	printf("%d \n", c);

	system("pause");
	return 0;
}

【结果运行提示dll丢失??】

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值