如何导出标准模板库(STL)类的实例化和包含STL类对象数据成员的类
概要
本文讨论如何实现下面任务:
- 导出标准模板库(STL)类的实例化。
- 导出包含STL类对象数据成员的类。
注意,您无法导出通用的模板,模板必须实例化才能导出。也就是说,必须提供所有的模板参数,并且在实例化时,模板的参数必须是完全定义的类型。例如stack<int>实例化STL堆栈类,实例化时强制生成类stack<int>的所有成员。
还要注意,一些STL容器(map、set、queue、list、deque)无法导出。
更多信息
从VC++ 5.0开始,可以强制对模板类进行实例化并导出实例化类。导出实例化模板类,可使用以下语法:
导出STL类
- 在
.dll和.exe文件中,必须链接到相同版本的C运行时库dll。两个都链接msvcrt.lib(Release版本)或都链接到msvcrtd.lib(Debug版本)。 - 在
dll中,在模板的实例化声明中使用__declspec修饰符,以便从dll中导出STL类的实例化。 - 在
exe中,需提供使用extern和__declspec修饰的模板实例化声明,以便从dll中导入类。 这会导致警告 C4231 "nonstandard extension used : 'extern' before template explicit instantiation."。 您可以忽略此警告。
导出包含STL类对象数据成员的类
- 在
.dll和.exe文件中,必须链接到相同版本的C运行时库dll。两个都链接msvcrt.lib(Release版本)或都链接到msvcrtd.lib(Debug版本)。 - 在
dll中,在模板的实例化声明中使用__declspec修饰符,以便从dll中导出STL类的实例化。
注意:不能跳过上一步,用于创建数据成员的STL类必须导出其实例化。 - 在
dll中,在类的声明中使用__declspec修饰符,以便从dll中导出类。 - 在
exe中,在类的声明中使用__declspec修饰符,以便从dll中导入类。
如果要导出的类有一个或多个基类,那么其基类也必须导出。如果要导出的类中还包含某类类型的数据成员,则还必须导出数据成员类型的类。
注意:一些STL类使用到其它的STL类,这些依赖的类也必须导出。如果使用低于1的警告级别(也就是/W2、/W3、/W4),则必须导出类会出现在编译器警告信息中。/W4级别编译时,会因为STL而生成大量警告信息,目前不推荐使用此级别警告。
一些STL类包含嵌套类,所以无法导出这些类。例如,deque包含一个嵌套类deque::iterator。如果导出deque,将产生警告信息。,提示你必须导出deque::iterator;如果你导出deque::iterator则会产生警告信息,提示你必须导出deque。这是受限于STL的设计,一旦模板类被实例化,它不能被重新实例化和导出。目前唯一可以导出的STL容器是vector,其它容器(map、set、queue、list、deque)都包含嵌套类无法导出。
导出使用用户定义类型(UDT)作为STL容器模板参数实例化的类时,必须为自定义类型()UDT)重载<和==运算符。例如,如果导出vector<MyClass>,则必须定义MyClass::operator<和MyClass::==。这是因为所有的STL容器类都具有成员比较运算操作,需要使用到包含类型的<和==运算操作。通常,这些都不被实例化,因为它们没有被使用。当实例化一个模板类时,会生成所有的成员函数,因为STL容器类具有使用到<和==的成员函数,所有必须实现它们。如果比较UDT的对象没有意义,也可以在定义operator<和opeartor==的时候,简单的返回true。
当在编译期间发现_DLL已经定义(当使用/MD或/MDd编译与C运行时库的DLL版本链接时,该符号被隐含定义),以下STL类以及对这些类的操作的各种全局运算符和函数,已在C运行时库DLL中导出。因此,无法从DLL中导出它们。z只要导入类也使用相同的C运行时DLL版本,则不应该导致可执行程序出问题。
Header STL template class
------------------------------
<IOSFWD> basic_ios
<IOSFWD> <IOSFWD>
<IOSFWD> basic_istream
<IOSFWD> basic_string (also typedef'd as string and wstring)
<IOSFWD> complex
<LOCALE> messages
<XLOCALE> codecvt
<XLOCALE> ctype
<XLOCMON> moneypunct
<XLOCMON> money_get
<XLOCMON> money_put
<XLOCNUM> numpunct
<XLOCTIME> time_get
<XLOCTIME> time_put
<XSTRING> basic_string (also typedef'd as string and wstring)
有关使用哪些模板参数以及声明哪些全局函数和操作符的具体细节,请参阅相关的头文件。
// -------------------------------------------
// MYHEADER.H
//disable warnings on 255 char debug symbols
#pragma warning (disable : 4786)
//disable warnings on extern before template instantiation
#pragma warning (disable : 4231)
#include <vector>
// Provide the storage class specifier (extern for an .exe file, null
// for DLL) and the __declspec specifier (dllimport for .an .exe file,
// dllexport for DLL).
// You must define EXP_STL when compiling the DLL.
// You can now use this header file in both the .exe file and DLL - a
// much safer means of using common declarations than two different
// header files.
#ifdef EXP_STL
# define DECLSPECIFIER __declspec(dllexport)
# define EXPIMP_TEMPLATE
#else
# define DECLSPECIFIER __declspec(dllimport)
# define EXPIMP_TEMPLATE extern
#endif
// Instantiate classes vector<int> and vector<char>
// This does not create an object. It only forces the generation of all
// of the members of classes vector<int> and vector<char>. It exports
// them from the DLL and imports them into the .exe file.
EXPIMP_TEMPLATE template class DECLSPECIFIER std::vector<int>;
EXPIMP_TEMPLATE template class DECLSPECIFIER std::vector<char>;
// Declare/Define a class that contains both a static and non-static
// data member of an STL object.
// Note that the two template instantiations above are required for
// the data members to be accessible. If the instantiations above are
// omitted, you may experience an access violation.
// Note that since you are exporting a vector of MyClass, you must
// provide implementations for the operator < and the operator ==.
class DECLSPECIFIER MyClass
{
public:
std::vector<int> VectorOfInts;
static std::vector<char> StaticVectorOfChars;
public:
bool operator < (const MyClass > c) const
{
return VectorOfInts < c. VectorOfInts;
}
bool operator == (const MyClass > c) const
{
return VectorOfInts == c. VectorOfInts;
}
};
// Instantiate the class vector<MyClass>
// This does not create an object. It only forces the generation of
// all of the members of the class vector<MyClass>. It exports them
// from the DLL and imports them into the .exe file.
EXPIMP_TEMPLATE template class DECLSPECIFIER std::vector<MyClass>;
// -------------------------------------------
// Compile options needed: /GX /LDd /MDd /D"EXP_STL"
// or: /GX /LD /MD /D"EXP_STL"
// DLL.CPP
#include "MyHeader.h"
std::vector<char> MyClass::StaticVectorOfChars;
// -------------------------------------------
// Compile options needed: /GX /MDd
// or: /GX /MD
// EXE.CPP
#include <iostream>
#include "MyHeader.h"
int main ()
{
MyClass x;
for (int i=0; i<5; i++) x.VectorOfInts.push_back(i);
for (char j=0; j<5; j++) x.StaticVectorOfChars.push_back('a' + j);
std::vector<int>::iterator vii = x.VectorOfInts.begin();
while (vii != x.VectorOfInts.end())
{
std::cout << *vii;
std::cout << " displayed from x.VectorOfInts" << std::endl;
vii++;
}
std::vector<char>::iterator vci = x.StaticVectorOfChars.begin();
while (vci != x.StaticVectorOfChars.end())
{
std::cout << *vci;
std::cout << " displayed from MyClass::StaticVectorOfChars";
std::cout << std::endl;
vci++;
}
std::vector<MyClass> vy;
for (i=0; i=5; i++) vy.push_back(MyClass());
return 1;
}
参考
其它相关信息,请在VC++帮助中搜索以下关键字:Explicit Instantiation(显示实例化)、__declspec、stack、/MD, /ML, /MT, /LD (Use Run-Time Library)。
如何导出标准模板库(STL)类的实例化和包含STL类对象数据成员的类的更多相关文章
- C++-标准模板库
C++较之C语言强大的功能之一是,C++编译器自带了大量的可复用代码库,我们称为标准模板库(standard template library),STL.标准模板库是一套常用的数据结构的集合,包括链表 ...
- C++标准模板库Stand Template Library(STL)简介与STL string类
参考<21天学通C++>第15和16章节,在对宏和模板学习之后,开启对C++实现的标准模板类STL进行简介,同时介绍简单的string类.虽然前面对于vector.deque.list等进 ...
- STL标准模板库(简介)
标准模板库(STL,Standard Template Library)是C++标准库的重要组成部分,包含了诸多在计算机科学领域里所常见的基本数据结构和基本算法,为广大C++程序员提供了一个可扩展的应 ...
- STL标准模板库介绍
1. STL介绍 标准模板库STL是当今每个从事C++编程的人需要掌握的技术,所有很有必要总结下 本文将介绍STL并探讨它的三个主要概念:容器.迭代器.算法. STL的最大特点就是: 数据结构和算法的 ...
- 【c++】标准模板库STL入门简介与常见用法
一.STL简介 1.什么是STL STL(Standard Template Library)标准模板库,主要由容器.迭代器.算法.函数对象.内存分配器和适配器六大部分组成.STL已是标准C++的一部 ...
- C++——string类和标准模板库
一.string类 1.构造函数 string实际上是basic_string<char>的一个typedef,同时省略了与内存管理相关的参数.size_type是一个依赖于实现的整型,是 ...
- STL 简介,标准模板库
这篇文章是关于C++语言的一个新的扩展--标准模板库的(Standard Template Library),也叫STL. 当我第一次打算写一篇关于STL的文章的时候,我不得不承认我当时低估了这个话 ...
- 标准模板库(STL)学习探究之vector容器
标准模板库(STL)学习探究之vector容器 C++ Vectors vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被 ...
- STL学习一:标准模板库理论基础
STL(Standard Template Library,标准模板库)是惠普实验室开发的一系列软件的统称.现然主要出现在C++中,但在被引入C++之前该技术就已经存在了很长的一段时间. STL的从广 ...
随机推荐
- 5G与TCP/IP
众所周知,4G LTE全面IP化,上层传输走的是TCP/IP协议(如下图). ▲LTE用户面协议构架 但是,我们熟悉的这个构架并没有发挥出移动网络的全部潜力,甚至阻碍了未来网络的发展. 1 TCP/ ...
- [转]PHP之APC缓存详细介绍(学习整理)
From : http://www.2cto.com/kf/201210/160140.html 1.APC缓存简介APC,全称是Alternative PHP Cache,官方翻译叫”可选PHP缓存 ...
- Guava之ImmutableMap使用示例
ImmutableMap 的作用就是:可以让java代码也能够创建一个对象常量映射,来保存一些常量映射的键值对. 分析以下情景,来具体讨论这个的好处. 假设现在有需求如下:根据数据库存的某个key字段 ...
- JEECG 命名规范
举例讲解代码规范 例如:表名 :jeecg_sys_demo 第一部分:代码文件命名规则如下: 首先:表名采用驼峰写法转换为Java代码使用单词 jeecg_sys_demo => Jeecg ...
- 算法 数组中出现次数最多的数字 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 汇总c#.net常用函数和方法集
1.DateTime 数字型 System.DateTime currentTime=new System.DateTime(); 1.1 取当前年月日时分秒 currentTime=System.D ...
- MFC中onmouseover与onmousemove的区别
onmouseover与onmousemove的区别是:当鼠标移过当前对象时就产生了onmouseover事件,当鼠标在当前对象上移动时就产生了onmousemove事件,只要是在对象上移动而且没有移 ...
- 大数据开发实战:Spark Streaming流计算开发
1.背景介绍 Storm以及离线数据平台的MapReduce和Hive构成了Hadoop生态对实时和离线数据处理的一套完整处理解决方案.除了此套解决方案之外,还有一种非常流行的而且完整的离线和 实时数 ...
- POJ 2280 Amphiphilic Carbon Molecules 极角排序 + 扫描线
从TLE的暴力枚举 到 13313MS的扫描线 再到 1297MS的简化后的扫描线,简直感觉要爽翻啦.然后满怀欣喜的去HDU交了一下,直接又回到了TLE.....泪流满面 虽说HDU的时限是2000 ...
- 微信小程序 this.data与this.setData
一.摘要 小程序中我们会经常使用到this.data与this.setData.其中this.data是用来获取页面data对象的,而this.setData是用来更新界面的.那么他们之间的区别与联系 ...