C++动态链接库:实现Fibonacci数字序列
作者:三味书屋 来源:三味书屋 2021/12/17 10:07:56

1、新建项目,命名为:MathLibrary

2、MathLibrary.h头文件代码

#ifdef MATHLIBRARY_EXPORTS
#define MATHLIBRARY_API __declspec(dllexport)
#else
#define MATHLIBRARY_API __declspec(dllimport)
#endif


// The Fibonacci 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 integral 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();


2、MathLibrary.cpp文件

// MathLibrary.cpp : Defines the exported functions for the DLL.
#include "pch.h" // use stdafx.h in Visual Studio 2017 and earlier
#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.
// Returns 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;
    }


    // Special case when index == 0, just return b value
    if (index_ > 0)
    {
        // otherwise, calculate next sequence value
        previous_ += current_;
    }
    std::swap(current_, previous_);
    ++index_;
    return true;
}


// Get the current value in the sequence.
unsigned long long fibonacci_current()
{
    return current_;
}


// Get the current index position in the sequence.
unsigned fibonacci_index()
{
    return index_;
}

3、知识点

3.1:MATHLIBRARY_EXPORTS 宏定义

定义 MATHLIBRARY_EXPORTS 宏时, MATHLIBRARY_API 宏在 __declspec(dllexport) 函数声明上设置 修饰符。 此修饰符指示编译器和链接器从 DLL 导出函数或变量,以便其他应用程序可以使用它。 

未定义 时MATHLIBRARY_API 宏在 __declspec(dllimport) 函数声明上设置 修饰符

查看项目属性,C/C++ --预处理器-- 预处理定义,VS已自动设置MATHLIBRARY_EXPORTS宏定义。

因此,编译时自动应用__declspec(dllexport)。

3.2:项目类型

查看项目属性,其实新建项目之初,VS已经自动把项目设置为:动态库


称      呼:
联系方式:
您的评论:
技术支持:l.w.dong@qq.com www.luweidong.cn
广州市   wx:lwdred
Copyright © 2014 三味书屋 All Rights Reserved
技术支持:l.w.dong@qq.com  sitemap xml  sitemap html

粤公网安备44010602011869号

粤ICP备13031080号-1