1)打开VC(我用的是VC6.0)文件->新建->Win32 Static Library 输入工程名称:Mylib
2)在工程里边新建C/C++ Header File和C++ Source File各一个,分别命名为:lib.h和lib.cpp
3)在lib.h中输入内容如下:
#ifndef _LIB_H
#define _LIB_H
#ifdef MYLIBAPI
#else
int MyFunc(int n);
#define MYLIBAPI extern "C" _declspec(dllimport)
#endif
#endif //_LIB_H
4)在lib.cpp中输入内容如下:
#define MYLIBAPI extern "C" _declspec(dllexport)
#include "lib.h"
int MyFunc(int n)
{
return n += 100;
}
5)先Compile(CTRL+F7),再Build(F7),现在可以查看在Debug文件中是否有Mydll.dll文件,如果没有,自己想办法(反正我是有的)
6)关闭刚才的工作空间,新建一个C++ SourceFile,命名为:Mytest.cpp(将刚才生成的Mydll.dll和dll.h拷贝到现在这个Mytest.cpp的文件目录)
文件内容如下:
#include <iostream>
#include "lib.h"
#pragma comment(lib, "Mylib.lib")
int main()
{
std::cout << MyFunc(8) << std::endl;
return 0;
}
7)这是运行的结果: