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 ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With