Solved

STL DLL (std::wstring)

Posted on 2001-07-10
8
1,953 Views
Last Modified: 2013-12-14
Problem: I have my CText class and CWordProperties Class, std::vector < CWordProperties> for my vector, and my vector is include in my CText Class, I try to export CText class with his vector outside the DLL.

This is my function who is export:
const CText __stdcall Parse (const std::wstring & wstrText, const in iLanguage);

I use std::wstring and std::string in my CWordProperties class. I overloade the operator < and ==.
This is what I got from the compiler.

Deleting intermediate files and output files for project 'ParseDLL - Win32 Debug'.
--------------------Configuration: ParseDLL - Win32 Debug--------------------
Compiling...
StdAfx.cpp
c:\parsedll\wordproperties.h(126) : warning C4251: 'm_strWord' : class 'std::basic_string<unsigned short,struct std::char_traits<unsigned short>,class std::allocator<unsigned short> >' needs to have dll-interface to be used by clients of class 'CWor
dProperties'
c:\parsedll\wordproperties.h(128) : warning C4251: 'm_strTag' : class 'std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' needs to have dll-interface to be used by clients of class 'CWordProperties'
c:\parsedll\text.h(148) : warning C4251: 'm_strId' : class 'std::basic_string<unsigned short,struct std::char_traits<unsigned short>,class std::allocator<unsigned short> >' needs to have dll-interface to be used by clients of class 'CText'
c:\program files\microsoft visual studio\vc98\include\xutility(50) : error C2678: binary '<' : no operator defined which takes a left-hand operand of type 'const class CWordProperties' (or there is no acceptable conversion)
        c:\program files\microsoft visual studio\vc98\include\vector(218) : see reference to function template instantiation 'bool __stdcall std::lexicographical_compare(const class CWordProperties *,const class CWordProperties *,const class CWordPr
operties *,const class CWordProperties *)' being compiled
c:\program files\microsoft visual studio\vc98\include\xutility(52) : error C2678: binary '<' : no operator defined which takes a left-hand operand of type 'const class CWordProperties' (or there is no acceptable conversion)
        c:\program files\microsoft visual studio\vc98\include\vector(218) : see reference to function template instantiation 'bool __stdcall std::lexicographical_compare(const class CWordProperties *,const class CWordProperties *,const class CWordPr
operties *,const class CWordProperties *)' being compiled
Error executing cl.exe.

ParseDLL.dll - 2 error(s), 3 warning(s)
0
Comment
Question by:fortineric
8 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 20 total points
Comment Utility
From the VC help.

**********************************************

HOWTO: Exporting STL Components Inside & Outside of a Class
Last reviewed: January 19, 1998
Article ID: Q168958  
The information in this article applies to:
Microsoft Visual C++, 32-bit Editions, version 5.0


SUMMARY
This article demonstrates how to:


Export an instantiation of a Standard Template Library (STL) class.

Export a class that contains a data member that is an STL object.

Note that you may not export a generalized template. The template must be instantiated, that is, all of the template parameters must be supplied and must be completely defined types at the point of instantiation. For instance "stack<int>;" instantiates the STL stack class. The instantiation forces all members of class stack<int> to be generated.


MORE INFORMATION
Beginning with Visual C++ 5.0, it is possible to force an instantiation of a template class and export the instantiation. To export a template class instantiation, use the following syntax:



To Export an STL Class

In both the DLL and the .exe file, link with the same DLL version of the C run time. Either link both with msvcrt.lib (release build) or link both with msvcrtd.lib (debug build).

In the DLL, provide the __declspec specifier in the template instantiation declaration to export the STL class instantiation from the DLL.

In the .exe file, provide the extern and __declspec specifiers in the template instantiation declaration to import the class from the DLL. This results in a warning C4231 "nonstandard extension used : 'extern' before template explicit instantiation". You can ignore this warning.

To Export a Class Containing a Data Member that is an STL Object

In both the DLL and the .exe file, link with the same DLL version of the C run time. Either link both with msvcrt.lib (release build) or link both with msvcrtd.lib (debug build).

In the DLL, provide the __declspec specifier in the template instantiation declaration to export the STL class instantiation from the DLL.
NOTE: You cannot skip step 2. You must export the instantiation of the STL class that you use to create the data member.


In the DLL, provide the __declspec specifier in the declaration of the class to export the class from the DLL.

In the .exe file, provide the __declspec specifier in the declaration of the class to import the class from the DLL.

NOTE: Some STL classes use other STL classes. These other classes must also be exported. The classes that must be exported are listed in compiler warnings if you compile with a warning level lower than 1; that is, /W2, /W3, or /W4. Warning level 4 generates a lot of warning messages for STL headers and is not currently recommended for that reason.
Some STL classes contain nested classes. These classes cannot be exported. For instance, deque contains a nested class deque::iterator. If you export deque, you will get a warning that you must export deque::iterator. If you export deque::iterator, you get a warning that you must export deque. This is caused by a designed limitation that once a template class is instantiated, it cannot be re-instantiated and exported.

Because of this limitation, make sure that you export your instantiations of the template classes before they are instantiated by any other code.

When you export an STL container parameterized with a user-defined type (UDT), you must define the operators < and == for your UDT. For example, if you export vector<MyClass>, you must define MyClass::operator < and MyClass operator ==. This is because all STL container classes have member comparison operators that require the existence of the operators < and == for the contained type. Normally, these are not instantiated because they are not used. When you instantiate an instance of a template class, all member functions are generated. Since the STL container classes have member functions that use the operators < and == for the contained type, you must implement them. If comparing objects of your UDT does not make sense, you can define the comparison operators to simply return true.?

When the symbol _DLL is defined during compiling (this symbol is implicitly defined when compiling with /MD or /MDd to link with the DLL version of the C Runtime), the following STL classes, and various global operators and functions that operate on these classes, are already exported by the C Runtime DLL. Therefore, you cannot export them from your DLL. This should not cause a problem for the executable program that imports your class as long as it also uses the DLL version of the C Runtime:


Header      STL template class
------      ------------------
<IOSFWD>    basic_ios
<IOSFWD>    basic_streambuf
<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)


For specific details on which template parameters are used and which global functions and operators are declared, please see the relevant header file.


Sample Code

   // -------------------------------------------
   // 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;
    }


0
 

Expert Comment

by:GUNDAFLORIDA
Comment Utility
listen....
0
 
LVL 3

Expert Comment

by:elcapitan
Comment Utility
?
0
 
LVL 1

Expert Comment

by:ccaprar
Comment Utility
The answers to your problem so far are already posted in your previous question ("DLL STL vector")
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Expert Comment

by:ccaprar
Comment Utility
For the warnings in lines 126 and 128 of wordproperties.h: you should link to the DLL version of the runtime library. It already exports string and wstring.

The same for warning in line 148 of text.h.

For errors in xutility, make sure you have defined CWordProperties::operator < and CWordProperties operator ==


0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Actually the other question was different and had a very different answer.  The other question asked

"How can I pass by parameter a vector from DLL to exe file?"

which is almost trivial.  You just need to make sure you use a shared DLL RTL.  

This answer is about errors that occur becainse a class experted from a DLL contains and STL data member which does not have the DLL interface.
0
 
LVL 11

Expert Comment

by:griessh
Comment Utility
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to accept "nietod" comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points.
The link to the Community Support area is: http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 5

Expert Comment

by:Netminder
Comment Utility
Per recommendation comment force/accepted by

Netminder
Community Support Moderator
Experts Exchange
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now