如何通过.def文件的方式导出类

看了许多资料,大部分都是通过__declspec(dllexport) 修饰符进行导出的。
通过.def文件导出函数和类都可以实现,但是导出模板函数和模板类却没有找到,但是没关系。

例子:

dll_test.h


#ifndef DLL_TEST_H
#define DLL_TEST_H


#define  DLL_EXPORT __declspec(dllexport)

#include<string>
using namespace std;


template<typename Dtype> class Operation
{
public:
    virtual Dtype getResult(Dtype a, Dtype b) = 0;
    virtual void destorySelf() { this->~Operation(); }
    virtual ~Operation() {};
};

template<typename Dtype> class OperationSub :public Operation<Dtype>
{
public:
    Dtype getResult(Dtype a, Dtype b);

};

template<typename Dtype> class OperationMul : public Operation<Dtype>
{
public:
    Dtype getResult(Dtype a, Dtype b);

};

//导出工厂函数
template<typename Dtype>  Operation<Dtype> *  createOperator(string oper); 

template<> DLL_EXPORT Operation<float> *  createOperator(string oper); //模板特化声明

template<> DLL_EXPORT Operation<double> *  createOperator(string oper); //模板特化声明


#endif

dll_test.cpp


#include"dll_test.h"


template<typename Dtype> Dtype OperationSub<Dtype>::getResult(Dtype a, Dtype b) { return (a - b); }
template<typename Dtype> Dtype OperationMul<Dtype>::getResult(Dtype a, Dtype b) { return (a * b); }


template<typename Dtype>  Operation<Dtype> *  createOperator (string oper) 
{
    if (strcmp(oper.c_str(), "-")== 0)
    {
        return new OperationSub<Dtype>();
    }
    else if (strcmp(oper.c_str(), "*") == 0)
    {
        return new OperationMul<Dtype>();
    }

}

// 模板特化 float 
template<> DLL_EXPORT Operation<float> * createOperator(string oper)
{
    if (strcmp(oper.c_str(), "-") == 0)
    {
        return new OperationSub<float>();
    }
    else if (strcmp(oper.c_str(), "*") == 0)
    {
        return new OperationMul<float>();
    }
}


// 模板特化 double
template<> DLL_EXPORT Operation<double> * createOperator(string oper)
{
    if (strcmp(oper.c_str(), "-") == 0)
    {
        return new OperationSub<double>();
    }
    else if (strcmp(oper.c_str(), "*") == 0)
    {
        return new OperationMul<double>();
    }
}

测试函数:
将上面的代码编译生成动态库dll,将文件.h,lib,dll添加到测试函数所在的工程。
test.cpp

#include"dll_test.h"
#include<string>
#include<iostream>
//#pragma comment(lib,"dll_test.lib")

int main()
{
    Operation<float> *foper = createOperator<float>("-");
    Operation<double> *doper = createOperator<double>("*");

    float fresult = foper->getResult(10, 5);
    double dresult = doper->getResult(10, 5);

    cout << fresult << endl;
    cout << dresult << endl;
}

参考文献:
1. http://forums.codeguru.com/showthread.php?364061-How-to-export-c-class-in-DLL-using-DEF-file
2. https://www.codeproject.com/Articles/28969/HowTo-Export-C-classes-from-a-DLL [HowTo: Export C++ classes from a DLL]
3. http://www.mingw.org/wiki/exporting_a_class_from_a_dll_for_multiple_instantiations

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值