一、静态库
静态库是编译期间链接的,能与应用程序形成一个一体化的可执行文件。
交付时只需提供静态链接库文件mySll.lib和头文件mySll.h即可。
头文件代码:
#ifndef MYSLL_H
#define MYSLL_H
extern "C"//作用是可在C中调用
{
int Factorial(int n);//申明函数原型
};
#endif
源文件代码:
#include "mySll.h"
#include <iostream.h>
void print()
{
cout<<"这是一个库提供的功能!"<<endl;
}
int Factorial(int n)
{
int Fac=1;
int i;
for (i=1;i<=n;i++)
{
Fac*=i;
}
print();
return Fac;
}
测试代码:
#include "mySll.h"
#include <iostream.h>
void main()
{
int f;
f=Factorial(10);
cout<<f<<endl;
}
二、动态库
交付时只需提供库文件DyDll.lib和编译所得dll文件DyDll.dll即可。
导出函数声明的两种方式:
1、模块定义文件(*.def)
2、关键字__declspec(dllexport)
源文件代码:
#include "DyDll.h"
#include <windows.h>
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)//入口函数
{
return TRUE;
}
int ShowHello(void)
{
MessageBox(NULL,"Hello, World!","",MB_OK);
return TRUE;
}
头文件代码:
extern "C"
{
__declspec(dllexport) int ShowHello(void);//用的第二种方式导出
};
测试代码(编译时需在工程设置中添加上DyDll.lib):
extern "C" int ShowHello(void);
//添加extern "C"修饰是因为原dll导出函数有extern "C"修饰,即C++编写的导出函数可在C中使用。
void main()
{
ShowHello();
}
原创文章如转载,请注明:转载自Eddy Blog
原文地址:http://www.rrgod.com/program/218.html 欢迎订阅Eddy Blog。