Exporting classes with STL members to a DLL

Get answers to all your basic programming questions. No Ogre questions, please!
Post Reply
User avatar
DWORD
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 1365
Joined: Tue Sep 07, 2004 12:43 pm
Location: Aalborg, Denmark
Contact:

Exporting classes with STL members to a DLL

Post by DWORD » Thu May 26, 2005 6:14 pm

Hi,

I'm facing a problem I've had before, and IIRC there was an elegant solution, but I may be wrong. I want to export some classes which have some STL strings and maps etc. as members from a DLL. I get this warning for all of them:

Code: Select all

warning C4251: 'Dingo::mStr' : class 'std::basic_string<_Elem,_Traits,_Ax>' needs to have dll-interface to be used by clients of class 'Dingo'
(...)
I searched google, and there are numerous hits, but the first many of them didn't really help. I found this one and the solution seems to be writing something like this (Microsoft specific?):

Code: Select all

#ifdef DINGO_EXPORTS
#   define DINGO_API __declspec(dllexport)
#   define DINGO_TEMPLATE_EXPORT
#else
#   define DINGO_API __declspec(dllimport)
#   define DINGO_TEMPLATE_EXPORT extern
#endif

DINGO_TEMPLATE_EXPORT template class DINGO_API std::basic_string<char>;
DINGO_TEMPLATE_EXPORT...
The problem is that this is also required for any classes used by the instantiated classes; i.e. std::basic_string<char> apparently depends on std::allocator<_Ty> being explicitly instantiated, too. As I have different std::maps and other stuff, this can potentially become a large number of instantiations I need to make.

Does anyone know how to fix this, and how does Ogre handle this, as it uses lots of STL?
0 x

User avatar
DWORD
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 1365
Joined: Tue Sep 07, 2004 12:43 pm
Location: Aalborg, Denmark
Contact:

Post by DWORD » Thu May 26, 2005 6:39 pm

Ok, I just found this in OgrePrerequisites.h:

Code: Select all

// disable: "<type> needs to have dll-interface to be used by clients'
// Happens on STL member variables which are not public therefore is ok
#   pragma warning (disable : 4251)
How does this affect functions where e.g. std::string or a const reference to std::string are returned or passed as parameters? In other words, is it safe to ignore (disable) this warning as long as I don't have any public members that are STL classes? I.e. would this be ok?

Code: Select all

class DINGO_API Dingo
{
public:
    Dingo(const std::string& str) : mStr(str) {}

    const std::string& getStr() const {return mStr; }
    std::string getStrCopy() const {return mStr; }

private:
    std::string mStr;
}
I'm just asking because I don't want any bad surprises, but of course I'm not doubting the Ogre source. :)
0 x

User avatar
DWORD
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 1365
Joined: Tue Sep 07, 2004 12:43 pm
Location: Aalborg, Denmark
Contact:

Post by DWORD » Thu May 26, 2005 8:19 pm

I think I'll just go with Ogre's approach. It works like a charm. Go and ignore this topic. :D

(If you have anything to add about what exactly is going on, I'd still be interested, though.)
0 x

User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
Contact:

Post by jacmoe » Thu May 26, 2005 8:21 pm

Well, I waded through a lot of information laden links regarding this topic some time ago, and I reached the exact conclusion as you:
Heck, if Ogre works ... 8)
0 x
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.

Post Reply