简体   繁体   English

带有模板的 dllexport 函数 (C++)

[英]dllexport function with templates (C++)

I would like to know whether it is possible to define a templated dllexport function.我想知道是否可以定义模板化的 dllexport 函数。 Until now the function was not templated, and it is working properly.到目前为止,该函数还没有被模板化,并且它工作正常。 The code was this:代码是这样的:

module.cpp:模块.cpp:

#if defined(__cplusplus)
    #define DLL_Export extern "C" __declspec(dllexport)
#else /* __cplusplus */
    #define DLL_Export __declspec(dllexport)
#endif /* __cplusplus */
...
VirtualTPM * virtual_tpm;
...
DLL_Export void TestPointModule_Check(string name, void * value){
    virtual_tpm->CheckTestPoint(name, value);   
}

However, I need to extend the TestPointModule_Check function to manage other datatypes, so I created a templated CheckTestPoint(...) function, and I tried this:但是,我需要扩展 TestPointModule_Check 函数来管理其他数据类型,所以我创建了一个模板化的 CheckTestPoint(...) 函数,我尝试了这个:

module.cpp:模块.cpp:

...
template <typename T>
DLL_Export void TestPointModule_Check(string name, void * value){
    virtual_tpm->CheckTestPoint<T>(name, value);    
}

but this gives me the following error: error C2988: unrecognizable template declaration/definition但这给了我以下错误:错误 C2988:无法识别的模板声明/定义

The VirtualTPM::CheckTestPoint<T> is correctly defined, since I can call a virtual_tpm->CheckTestPoint<int>(name, value); VirtualTPM::CheckTestPoint<T>已正确定义,因为我可以调用virtual_tpm->CheckTestPoint<int>(name, value); without errors.没有错误。

Is there a possible way to do what I need?有没有可能的方法来做我需要的? I would be able to restrict the template to 4 different datatypes (in fact, I did this when defining the template for the VirtualTPM::CheckTestPoint<T> function, but I don´t know how to do it.我将能够将模板限制为 4 种不同的数据类型(实际上,我在为VirtualTPM::CheckTestPoint<T>函数定义模板时这样做了,但我不知道该怎么做。

Templated functions does not exist until they are instantiated.模板化函数在实例化之前不存在。 So that means that you can export only its instances not the template itself.因此,这意味着您只能导出其实例,而不能导出模板本身。

But you if you put the template definition in a header you can normally use it in other projects - same as you would do for header-only libs.但是,如果您将模板定义放在标题中,您通常可以在其他项目中使用它 - 就像您对仅标题库所做的一样。

Edit: Untested sample (this should export the function from DLL):编辑:未经测试的示例(这应该从 DLL 导出函数):

module.h:模块.h:

template <typename T>
DLL_Export void TestPointModule_Check(string name, T * value);

module.cpp:模块.cpp:

template <typename T>
DLL_Export void TestPointModule_Check(string name, T * value){
  virtual_tpm->CheckTestPoint<T>(name, value);    
}

// explicit instantiation
template void TestPointModule_Check<SomeType>(string name, SomeType * value);

In addition to what @Jhonny already pointed out You should be able to say:除了@Jhonny已经指出的内容之外,您应该能够说:

...
template <>
DLL_Export void TestPointModule_Check<AParticularType>
                    (string name, AParticularType* value) {
    virtual_tpm->CheckTestPoint<AParticularType>(name, value);  
}

Put to header:放入标题:

template<typename T>
void TestPointModule_Check(string name, T* value);

and:和:

template <>
DLL_Export void TestPointModule_Check<AParticularType>
                    (string name, AParticularType* value);

Also overwork your macro definitions to export from c++:还要过度使用您的宏定义以从 C++ 导出:

#if !defined(__cplusplus)
//  ^ Note
#define DLL_Export extern "C" __declspec(dllexport)
#else /* __cplusplus */
#define DLL_Export __declspec(dllexport)
#endif /* __cplusplus */

To find additional information about the topic refer to this link or this one .要查找有关该主题的更多信息请参阅本链接或本一个

Not sure whether the other answers work, but I could not make them work.不确定其他答案是否有效,但我无法让它们起作用。 This is what should be done, with an example (the function template can generalize to any kind of template function):这是应该做的,举个例子(函数模板可以推广到任何类型的模板函数):

In *.hpp :*.hpp

template <typename T>
int randomFunction(T templateElement);

In *.cpp :*.cpp

template <typename T>
int randomFunction(T templateElement)
{
    // code...
}
template DLL_EXPORT_IMPORT int randomFunction(float templateElement);
template DLL_EXPORT_IMPORT int randomFunction(double templateElement);
// Any other types you wanna implement...

And where DLL_EXPORT_IMPORT should be defined once in your library as something like:并且DLL_EXPORT_IMPORT应该在您的库中定义一次,如下所示:

#ifndef _WIN32
    #define DLL_EXPORT_IMPORT
#elif defined OP_EXPORTS
    #define DLL_EXPORT_IMPORT __declspec(dllexport)
#else
    #define DLL_EXPORT_IMPORT __declspec(dllimport)
#endif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM