1、打开VS2017,新建一个"动态链接库(DLL)"项目,这里命名为“MyFirstDll”
2、建好后的项目中,在头文件中会自动生成framework.h和pch.h两个文件,在源文件中会自动生成dllmain.cpp和pch.cpp两个文件
3、在头文件中新建一个MathLibrary.h文件,用于函数的声明。
在源文件中新建一个MathLibrary.cpp文件,用于函数的实现。
代码用例是实现Fibonacci生兔子的例子
// MathLibrary.h
#pragma once
// import 进口,输入
// export 出口,输出
#ifdef MATHLIBRARY_EXPORTS
#define MATHLIBRARY_API __declspec(dllexport)
#else
#define MATHLIBRARY_API __declspec(dllimport)
#endif
// The Finoacci recurrence relation describes a sequence F
// where F(n) is { n = 0, a
// { n = 1, b
// { n >1, F(n-2) + F(n-1)
// for some initial intergral values a and b.
// If the sequence is initialized F(0) = 1, F(1) = 1,
// then this relation produces the well-known Fibonacci
// sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
// initialize a Fibonacci relation sequence
// such that F(0) = a, F(1) = b.
// this function must be called before any other function
extern "C" MATHLIBRARY_API void fibonacci_init(const unsigned long long a, const unsigned long long b);
// produce the next value in the sequence .
// returns true on success and updates current value and index;
// false on overflow, leaves current value and index unchanged.
extern "C" MATHLIBRARY_API bool fibonacci_next();
// get the current value in the sequence.
extern "C" MATHLIBRARY_API unsigned long long fibonacci_current();
// get the position of the current value in the sequence
extern "C" MATHLIBRARY_API unsigned fibonacci_index();
// MathLibrary.cpp: Defines the exported function for the Dll
// #include"stdafx.h"
#include"pch.h"
#include<utility>
#include<limits.h>
#include"MathLibrary.h"
// Dll internal state variables:
static unsigned long long previous_; // previous value, if any
static unsigned long long current_; // current sequence value
static unsigned index_; // current seq. Position
// initialize a fibonacci relation sequence
// such that F(0) = a, F(1) = b.
// This function must be called before any other function.
void fibonacci_init(const unsigned long long a, const unsigned long long b)
{
index_ = 0;
current_ = a;
previous_ = b; // see special case when initialized
}
// produce the next value in the sequence
// return true on success, false on overflow.
bool fibonacci_next()
{
// check to see if we'd overflow result or position
if ((ULLONG_MAX - previous_ < current_) || (UINT_MAX == index_))
{
return false;
}
if (index_ > 0)
{
previous_ += current_;
}
std::swap(current_, previous_);
++index_;
return true;
}
unsigned long long fibonacci_current()
{
return current_;
}
unsigned fibonacci_index()
{
return index_;
}
我这里是在realease x64下运行,所以在与项目名同目录下的x64中会生成一个release文件夹
测试dll文件
1、打开VS2017新建一个空项目,命名为testDll2
2、将之前生成的MyFirstDll.dll、MyFirstDll.lib和MathLibrary.h文件复制到项目文件中。
3、在testDll2中的头文件夹中,将MathLibrary.h添加进来、在资源文件中将MyFirstDll.lib添加进来。
4、开始测试用例,在testDll2.cpp中添加如下代码:
#include <iostream>
#include"MathLibrary.h"
int main()
{
std::cout << "Hello World!\n";
fibonacci_init(1, 1);
do
{
std::cout << fibonacci_index() << ":"
<< fibonacci_current() << std::endl;
} while (fibonacci_next());
// report count of values written before overflow
std::cout << fibonacci_index() + 1 <<
"Fibonacci sequence values fit in an " <<
"unsigned 64-bit interger. " << std::endl;
}
.dll和.lib文件是在release x64下生成的,所以,运用时也需要在release x64下运行。运行结果: