DLL导出STL模板类和成员变量
2017-05-19 18:19
162 查看
// ------------------------------------------- // 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; }
转自:https://my.oschina.net/u/2424583/blog/524001
相关文章推荐
- DLL导出STL模板类和成员变量
- DLL中导出STL模板类的问题
- DLL导出实例化的模板类
- STL导出DLL 警告 C4251 warning when using stl-classes in the dll-interface
- 在dll中导出模板类的方法
- 一个获取dll导出函数的模板类
- DLL中导出STL时,去掉警告的方法
- 自定义类,导出dll,stl容器vector
- [百度空间] [原]DLL导出实例化的模板类
- 模板类和模板函数在dll导出
- dll中导出一个类.导出模板STL.vector,遭遇的警告.
- VS编译器从DLL导出模板类
- vc 的dll可以导出模板类的具体子类,但是这个模板类中不能用static变量
- DLL中导出函数(函数名及其调用约定)??
- STL与BOOST学习基础之模板函数和模板类.
- Excel-数据分类导出至多个Sheet NPOI.dll
- C++ 类函数导出DLL,多类dll嵌套。
- STL中的模板类pair 和map
- 使用def文件简化dll导出
- Aspose.Cells.dll引用导入导出Excel