#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;
}