Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling functions with STL parameters from a shared library

I know that it's not possible to safely export a function C++ parameters (eg STL strings), because C++ does not specify a standard ABI. (I've read that as an answer to How to call a function from a shared library? )

People tend to believe that if both your library and your program have been built with the same compiler then this isn't an issue, but unfortunately, for some VC++ compilers, this is not entirely true.

Herb Sutter and Alexandrescu in "C++ Coding Standards" propose to rely on portable types (eg built-in types) instead of a function that takes a string.

ie instead of using std::string in a module interface

 std::string Translate( const std::string & );

use a

void Translate( const char *src, char* dest, size_t destSize );

Although they agree this is fairly complex for both the caller and the callee, they don't propose a nicer alternative.

How can one nicely pass a more complex object, such as a std::map using low-level types? (not to mention something as complex as map<string,vector<something_complex> >)

How do you tackle such cases when exporting functions ?

like image 217
Grim Fandango Avatar asked Apr 12 '11 15:04

Grim Fandango


1 Answers

Not only do you have to have built the program using the same compiler (or compatible ABI), you need to have used the same settings: debug & release mode, DLL and Static mode, checked iterators (or not), and so on. In one respect, STLPort did it right: the std::string when building for their library is renamed into distinct symbols based on these settings, avoiding the issue entirely.

The other option is to pass along opaque pointers to objects allocated, disposed, and used within the DLL. In effect, making your own virtual interface to the functions you need, if you want to use them as objects outside the DLL.

like image 162
Andy Finkenstadt Avatar answered Sep 18 '22 14:09

Andy Finkenstadt