简体   繁体   English

从 dll 导入显式实例化的模板 class

[英]Importing explicitly instantiated template class from dll

Being a dll newbie I have to ask the allmighty SO about something.作为一个 dll 新手,我不得不向全能的 SO 询问一些事情。

Say I explicitly instantiate a template class like this:假设我像这样显式实例化模板 class :

template class __declspec(dllexport) B<int>;

How do I use import this templated class again?如何再次使用导入此模板 class?

I've tried the adding the code below in my.cpp file where I want to use B我已经尝试在我想使用 B 的 my.cpp 文件中添加下面的代码

template class __declspec(dllimport) B<int>;

When you instantiate a template fully -- you have a complete type.当你完全实例化一个模板时——你就有了一个完整的类型。 It is no different from any other types.它与任何其他类型没有什么不同。 You need to include the header for B and also compile-time linking in with a lib file or dynamically load the dll to link to the definition.您需要包含用于B的 header 并且还需要使用lib文件进行编译时链接或动态加载 dll 以链接到定义。

Have you read this article: http://support.microsoft.com/kb/168958 ?您是否阅读过这篇文章: http://support.microsoft.com/kb/168958

Here's a brief summary of what I tested (and it worked):这是我测试的内容的简要摘要(并且有效):


Create a dummy DLL project创建一个虚拟 DLL 项目

  • Used the Win32 Console application wizard to generate the dll header/source files called: template_export_test使用 Win32 控制台应用程序向导生成 dll 头文件/源文件,称为: template_export_test
  • Added the following:添加了以下内容:

file: template_export_test.h文件: template_export_test.h


#ifndef EXP_STL
#define EXP_STL
#endif 

#ifdef EXP_STL
#    define DECLSPECIFIER __declspec(dllexport)
#    define EXPIMP_TEMPLATE
#else
#    define DECLSPECIFIER __declspec(dllimport)
#    define EXPIMP_TEMPLATE extern
#endif

EXPIMP_TEMPLATE template class DECLSPECIFIER CdllTest<int>;

file: template_export_test.cpp文件: template_export_test.cpp


template<class T>
CdllTest<T>::CdllTest(T t)
: _t(t)
{
    std::cout << _t << ": init\n";
}

Create the test application创建测试应用程序

  • Use the wizard to create a Win32 Console application called: driver使用向导创建名为: driver的 Win32 控制台应用程序
  • Edit the Linker project settings of this project:编辑本工程的Linker工程设置:
    • Add to Linker > General > Additional Library Directories: path to template_export_test.lib添加到 Linker > 常规 > 其他库目录: template_export_test.lib的路径
    • Add to Linker > Input > Additional Dependencies: template_export_test.lib添加到 Linker > 输入 > 附加依赖项: template_export_test.lib
  • Include the template_export_test.h in the main cpp file在主 cpp 文件中包含template_export_test.h

#include "c:\Documents and Settings\...\template_export_test.h"
using namespace std;

int main(int argc, char** argv) {
    CdllTest<int> c(12);
}

  • Compile and go!编译并开始!

It seems, even with explicit instatiation of the template, problems may arise that lead to run-time errors.看起来,即使模板的显式实例化,也可能出现导致运行时错误的问题。 Take a look at this interesting article to C4251 (especially the "Conclusion").看看这篇关于 C4251 的有趣文章(尤其是“结论”)。

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

相关问题 如何在没有警告的情况下使用从 dll 中显式实例化的模板派生的 dllexport-ed 类? - How to use a dllexport-ed class which is derived from an explicitly instantiated template in a dll without warnings? 如何在Visual Studio中导出从显式实例化模板派生的类? - How to export a class derived from an explicitly instantiated template in Visual Studio? 显式实例化模板类的显式实例化模板方法 - Explicitly instantiate template method of explicitly instantiated template class 显式实例化类模板中的自动构造函数 - Automatic constructor in explicitly instantiated class template 除非显式专用,否则未实例化模板类的静态成员? - Static member of template class not instantiated unless explicitly specialized? 从基类转换为实例化的类模板 - Casting from Base Class to Instantiated Class Template 未定义的对显式实例化模板函数的引用 - Undefined reference to explicitly instantiated template function 显式实例化模板方法中的编译错误 - Compilation error in explicitly instantiated template methods 将显式实例化的函数模板与转换匹配 - Matching explicitly instantiated function template with conversion 在实例化类时是否实例化了类模板的成员? - Are members of a class template instantiated when the class is instantiated?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM