Jump to content

  • Log In with Google      Sign In   
  • Create Account


Exporting and the issues that transpire!


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 CRYP7IK   Members   -  Reputation: 853

Like
0Likes
Like

Posted 10 January 2013 - 12:13 AM

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?


To accomplish great things we must first dream, then visualize, then plan...believe... act! - Alfred A. Montapert
Gold Coast Studio Manager, Lead Programmer and IT Admin at Valhalla Studios Bifrost.

Sponsor:

#2 wqking   Members   -  Reputation: 756

Like
0Likes
Like

Posted 10 January 2013 - 12:20 AM

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.


http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.


#3 Hodgman   Moderators   -  Reputation: 28421

Like
2Likes
Like

Posted 10 January 2013 - 12:30 AM

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.



#4 BornToCode   Members   -  Reputation: 912

Like
1Likes
Like

Posted 10 January 2013 - 01:58 AM

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, 10 January 2013 - 02:00 AM.


#5 Schrompf   Prime Members   -  Reputation: 950

Like
0Likes
Like

Posted 10 January 2013 - 02:01 AM

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, 10 January 2013 - 02:04 AM.

----------
Gonna try that "Indie" stuff I keep hearing about. Let's start with Splatter.

#6 RobTheBloke   Crossbones+   -  Reputation: 2324

Like
0Likes
Like

Posted 11 January 2013 - 10:02 AM

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, 11 January 2013 - 10:04 AM.


#7 RobTheBloke   Crossbones+   -  Reputation: 2324

Like
0Likes
Like

Posted 11 January 2013 - 10:08 AM

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

 

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).






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS