Linux: C++ Abstract class as shared object API -
i read this article on c++ dll api-s. "c++ mature approach: using abstract interface" work on linux different compilers (exe , .so compiled different compilers)? couldn't find online confirms/denies linux systems. in article author says works windows because com technology works other compilers.
to understand question please read @ least "c++ mature approach" chapter of article.
yes , no.
there's no guarantee different compilers implement virtual function calls in returned interface class in same way - , if case fail catastrophically (or silently corrupt stuff ... more fun). if recal correctly - modern g++, clang , intel on linux seem handle same way , should interoperable @ level.
however there's further gotcha not mentioned in article, , applies both linux , windows. if take approach only things can pass
- interfaces
- simple types int
- types control in memory layout.
this means cant have function in interface void withvector(std::vector<int> v)
since different standard libraries of compilers may layout internals of vector differently. in fact can change between compiler versions, standard library versions, , compiler settings.
so you'll need create iintvector
wraps std::vector
, use withvector(iintvector& v)
.
you can run same issues if pass or return non-interface classes of own.
an old example bit me passing boost::shared_ptr
between compilation units, in 1 case lock member in class , in other no lock existed - means objects had different expected sizes , resulted in (silent) stack corruption. fun whole development team.
i've found it's less error-prone use pure c bridge between 2 sides , provide utility layer in c++ dll creator can build , use calls pure c bridge. in c not trivial.
in c case still need watch out changes in data structures caused compiler settings (but rarer) , need careful 1 compiler doesn't insert padding structures other not.
Comments
Post a Comment