Sign in to follow this  
sandy

STL in a DLL

Recommended Posts

I found a few other people who posted about this, but the threads never seemed to end with a solution. I'm trying to use STL (i.e. std::string, std::vector, etc...) inside a DLL. However I always seem to get errors about std::insertnamehere not being exported. I've search all over for a soultion to this and have found a lot of information. I've followed instructions on msdn and in threads here to the letter but nothing seems to work. I can remove one error only to create more... I'm beginning to think that I just can't use STL in a DLL... Is this the case?

Share this post


Link to post
Share on other sites
No, you can use STL containers in DLLs, but it's not worth the effort (at least in my opinion). First off you have to make sure that the application using the DLL is compiled with the same compiler and STL version (usually not a problem), then you have to make sure that both the application and the DLL share the C runtime. In MSVC this accomplished by building both EXE and DLL with the Multithreaded DLL version of the C Runtime, or the Multithreaded Debug DLL version of the C Runtime for a debug build. Then you need to make sure to export the STL class from the DLL properly. Here is an article describing the syntax necessary. Also if you use custom allocators, you have to make sure any static members of the allocators are exported properly too.

Edit: Also, if you have multiple DLLs using the STL classes, you should then either create a new DLL that the other DLLs use to import the STL classes from, or otherwise mark one of the DLLs as holding the master copies of the STL code.

Share this post


Link to post
Share on other sites
Simply switching to stlport does most of that for you, but you will still have to keep your build settings consistent wether you are using the stl or not.

Share this post


Link to post
Share on other sites
I don't understand what you mean, I am using the standard STL (for the lack of being lazy and not switching to stlport yet) and it works perfectly fine in a DLL. All you need to do is simply include the header files, and boom, it works like magik. But I also don't use multiple DLLs (yet) and don't pass STL containers across DLLs.

Share this post


Link to post
Share on other sites
You can use STL perfectly fine in an DLL. The problems arise when you pass STL containers across DLL boundaries and (try to) modify the contents.

If you really need that you can provide a custom allocator to those containers which uses GlobalAlloc for example.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this