Sign in to follow this  
CRYP7IK

Exporting and the issues that transpire!

Recommended Posts

CRYP7IK    1327

Hey all,

 

I would like to start a discussion on the ever present issue that I am sure all programmers must face at some point, the issue or warning of exporting classes that contain standard template library classes; for example std::vector and std::string.  What do you think the most efficient, platform agnostic and cost effective solution is?

 

The first solution that comes to my mind is to just re-write aspects of the STL that you want to use.

 

Pros:

  • Full control
  • Probably faster (But situational)
  • No problems exporting your own classes
  • Can be thread friendly
  • Easier to debug

Cons:

  • Very cost in-effective
  • Needs a lot of testing

 

Another solution is to just ignore it and build different libraries for different implementations, so use msvc, gcc, clang and name your libraries after what they were compiled with. E.g. MyLibWindows64MSVC or MyLibWindows32GCC

 

Pros:

  • Very cost effective
  • Full usage of stl

Cons:

  • Hard for clients to use

 

How have you solved this problem or how would you solve this problem?

Share this post


Link to post
Share on other sites
wqking    761

Since there is no "correct" answer, I will share my experience.

 

I only use STL to implement the library, I don't expose STL to public API.

 

If a function returns a string, I will return a char * rather than std::string.

 

Also even if STL is platform independent, I would not expose a function to return a list or vector.

If you return a list or vector, the user can do much more than you expect, such as insert an invalid object, etc.

I would simply wrap a list or vector to simple non-template class to access the elements and element count.

Share this post


Link to post
Share on other sites
Hodgman    51234

[quote name='CRYP7IK' timestamp='1357798392' post='5019784']
Another solution is to just ignore it and build different libraries for different implementations, so use msvc, gcc, clang and name your libraries after what they were compiled with. E.g. MyLibWindows64MSVC or MyLibWindows32GCC
[/quote]You have to do that anyway -- the C++ ABI (e.g. name mangling) isn't portable across compilers, so a library built by one compiler very likely just wont work with another compiler, no matter how simple/complex the interface is.

 

Personally, if I can't get the source to a C++ library that I want to use, then I don't use it. Even if I can get a binary that was built for my compiler, maybe I'll want to switch compilers later and the library author will have disappeared, leaving me stuck.

Share this post


Link to post
Share on other sites
BornToCode    1185

Another solution is to just ignore it and build different libraries for different implementations, so use msvc, gcc, clang and name your libraries after what they were compiled with. E.g. MyLibWindows64MSVC or MyLibWindows32GCC

You have to do that anyway -- the C++ ABI (e.g. name mangling) isn't portable across compilers, so a library built by one compiler very likely just wont work with another compiler, no matter how simple/complex the interface is.

 

 

Personally, if I can't get the source to a C++ library that I want to use, then I don't use it. Even if I can get a binary that was built for my compiler, maybe I'll want to switch compilers later and the library author will have disappeared, leaving me stuck.

But that is not an luxury that you always get.

Edited by BornToCode

Share this post


Link to post
Share on other sites
Schrompf    1035
Personally, if I can't get the source to a C++ library that I want to use, then I don't use it. Even if I can get a binary that was built for my compiler, maybe I'll want to switch compilers later and the library author will have disappeared, leaving me stuck.
That. Even with a library built exactly for the compiler I'm currently using, some optimization flag set to a different value is already enough to render the library unlinkable. Give me the source and don't make a fuss about it.

Luckily there's a lot of OpenSource libraries out there, and all the "I keep my precious code to myself" people are slowly dieing out. Code is overrated, especially the code of people who think it's precious.

[Edit] Of course sometimes you don't have a choice. And if you stick to C and C only for the interface, it will work. But that's not the topic. The topic was "export STL stuff", and that's only viable if you have the source code. Edited by Schrompf

Share this post


Link to post
Share on other sites
RobTheBloke    2553
You have to do that anyway -- the C++ ABI (e.g. name mangling) isn't portable across compilers, so a library built by one compiler very likely just wont work with another compiler, no matter how simple/complex the interface is.

For a well defined API, this should never cause a problem. Now if you're going to start passing references to std::string / std::vector<> across your DLL boundary, then you're going to have issues. For a dll that defines a single C entry point (e.g. CreateMySDK()), then uses nothing but virtual interfaces (or infact, any C++ class that restricts communication to functions only, again making sure that the data types you pass into args are of a known size), then you should have no problem.

Every compiler includes some form of libtool to generate a link library for a DLL compiled with another compiler. It's not an impossible task....

That. Even with a library built exactly for the compiler I'm currently using, some optimization flag set to a different value is already enough to render the library unlinkable. Give me the source and don't make a fuss about it.
Which means you have been ignoring warning C4251 (i.e. you have a function arg, return type, or member var, in your DSO interface that is being statically linked in from another library. This is bad news!) . If you fix all of those warnings, the problems you describe will never happen. Edited by RobTheBloke

Share this post


Link to post
Share on other sites
RobTheBloke    2553

[quote]The first solution that comes to my mind is to just re-write aspects of the STL that you want to use.[/quote]

 

The best solution is to stop passing STL containers across the DLL boundary. Ensure that you can only heap allocate your DLL objects (e.g. use a static create() method, rather than leaving the ctor as public). Finalize your classes with the DLL (private ctor / copy ctor). Make sure all member variables are private, and that all of your get/set methods have been DLL exported (also make sure you export the ctor/dtor/copy ctor).

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