How to export STL classes in DLL

Source: Internet
Author: User

From http://www.vckbase.com/

Author: yy2better

Introduction: This article describes how to export STL classes and methods that contain STL classes in DLL. Example source code

DLL cannot directly export the generic template (generalized template). Therefore, if you want to export the STL class, the template must be instantiated first (instantiated ). In addition, if the exported STL class uses other STL classes, these other classes must be exported at the same time. Currently, the only container in STL that can be exported is vector. Other containers (such as map, set, queue, list, And deque) cannot be exported because they contain Nested classes.
To export the STL class:

  1. In the DLL and exe files, use the same version of the C Runtime Library Link. For example, you can use the msvcrt. Lib (release) link or the msvcrtd. Lib (Debug) link.
  2. DLL, use _ declspec (dllexport) to export the template class instance.
  3. In the EXE file, use the _ declspec (dllimport) and extern keywords to import STL classes from the DLL.
    Note that when you export an STL container with a custom class as the template parameter, you must define the <and = operators for this custom type. For example, if you want to export the STD: vector <cperson> class, you must add the <and = operators for cperson. As follows:
// Export the STL class STD: vector <cperson> class cperson {public: int m_nage; char m_strname [40]; public: bool operator <(const cperson & C) const {return true;} bool operator = (const cperson & C) const {return true ;}}; expimp_template template class vecdll_api STD :: vector <cperson> // display the instantiated template class vecdll_api int fnvecdll (STD: vector <cperson> & vecper); // export the Function

The reason for defining these two operators is that all STL containers have "Compare" member functions, which need to call the <and = operators of the custom type. In general, since these member functions are not used, they are not instantiated, So we generally do not need to define these two operators for cperson when using them. However, when it is displayed that this container class is instantiated, all its member functions need to be instantiated, including its "Compare" member functions. Therefore, cperson's <and = operators must be implemented at this time. If cperson does not care about the meaning of <and =, we can implement them by simply returning true as shown in the code above.

Export a "data member contains STL objects" class. The method is similar to above. The following code is used:

Expimp_template template class vecdll_api STD: vector <int> // display the instantiation STD: vector <int> class vecdll_api ccontainer {public: STD: vector <int> m_vecnum ;};

For the complete example, see the instance code.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.