最简单的例子,过程如下:
1:建立一个控制台空工程
2:添加3个文件:dll_01.h,dll_01.cpp,dll_01.def;
3:在dll_01.h中如是写:
#ifndef DLL_01_H_ //这2句不用说了吧
#define DLL_01_H_
#if defined DLL_EXPORT //在dll_01.cpp中会define DLL_EXPORT
#define DECLDIR _declspec(dllexport)
#else
#define DECLDIR _declspec(dllimport)
#endif
extern "C"
{
DECLDIR int add(int a,int b); //声明
};
#endif
4:在dll_01.cpp中如是写:
#define DLL_EXPORT
#include "dll_01.h"
extern "C"
{
DECLDIR int add(int a,int b)
{
return a+b;
}
};
5:在dll_01.def中如是写:
LIBRARY "dll_01"
EXPORTS ; Export our functions
add @1
好了,在测试工程Test中添加dll_01.lib,dll_01.dll,dll_01.h这3个文件,Test.cpp代码如下:
#pragma comment(lib,"dll_01.lib")
#include "dll_01.h"
#include <iostream>
using namespace std;
int main()
{
cout<<add(1,2)<<endl;
return 0;
}
工作完成!