从dll导入显式实例化的模板类 [英] Importing explicitly instantiated template class from dll

查看:314
本文介绍了从dll导入显式实例化的模板类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



说明我明确地实例化了一个这样的模板类:

 模板类__declspec(dllexport)B< int> ;; 

如何再次使用导入此模板类?



尝试添加下面的代码在我的.cpp文件中我想使用B

 模板类__declspec(dllimport)B< int>; 


解决方案

完全实例化一个模板 - 你有一个完整类型。与任何其他类型没有任何区别。您需要包含 B 的标题,还可以使用 lib 文件编译时链接,或动态加载dll链接到定义。



您是否阅读过这篇文章: http://support.microsoft.com/kb/168958



这是一个简短的摘要,我测试了(它的工作):






创建一个虚拟DLL项目




  • 使用Win32控制台应用程序向导生成名为$ code的dll标题/源文件template_export_test

  • 添加以下内容:



文件: template_export_test.h






  #ifndef EXP_STL 
#define EXP_STL
#endif

#ifdef EXP_STL
#define DECLSPECIFIER __declspec(dllexport)
#define EXPIMP_TEMPLATE
#else
#define DECLSPECIFIER __dec lspec(dllimport)
#定义EXPIMP_TEMPLATE extern
#endif

EXPIMP_TEMPLATE模板类DECLSPECIFIER CdllTest< int&






file: template_export_test。 cpp






 模板< class T> 
CdllTest< T> :: CdllTest(T t)
:_t(t)
{
std :: cout< _t< :init\\\
;
}






创建测试应用程序




  • 使用向导创建一个名为code驱动程序的Win32控制台应用程序$ c>

  • 编辑此项目的链接器项目设置:


    • 添加到链接器>常规>附加库目录:路径到 template_export_test.lib

    • 添加到链接器>输入法>附加依赖关系: template_export_test.lib


  • 在主cpp中包含 template_export_test.h 文件






  #includec:\\ \\ Documents and Settings \ ... \template_export_test.h
using namespace std;

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







  • 编译并去!


Being a dll newbie I have to ask the allmighty SO about something.

Say I explicitly instantiate a template class like this:

template class __declspec(dllexport) B<int>;

How do I use import this templated class again?

I've tried the adding the code below in my .cpp file where I want to use B

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.

Have you read this article: http://support.microsoft.com/kb/168958 ?

Here's a brief summary of what I tested (and it worked):


Create a dummy DLL project

  • Used the Win32 Console application wizard to generate the dll header/source files called: template_export_test
  • Added the following:

file: 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<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
  • Edit the Linker project settings of this project:
    • Add to Linker > General > Additional Library Directories: path to template_export_test.lib
    • Add to Linker > Input > Additional Dependencies: template_export_test.lib
  • Include the template_export_test.h in the main cpp file

#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!

这篇关于从dll导入显式实例化的模板类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆