[swift-users] Using C++ libraries in Swift projects?
harlydavidsen at gmail.com
Fri Jan 13 11:37:11 CST 2017
So you don't know how I could directly port this to Swift--or make an
On 1/13/17, Jens Alfke <jens at mooseyard.com> wrote:
>> On Jan 13, 2017, at 8:18 AM, Ethin Probst via swift-users
>> <swift-users at swift.org> wrote:
>> I was wondering how I could go about using the Boost library project
>> (among others) in swift?
> No, it’s generally not feasible to use C++ libraries from any other
> language. The main reasons are that (a) name-mangling of C++ functions means
> that there’s no reliable way to tell the other language the name of the
> function you want to call, and (b) C++ has so many language features that
> are tightly entwined with the way you call functions — constructors,
> copying, assignment, references, implicit conversions, operator overloading,
> In the simple example you gave there are several warning signs:
> * BOOST_LOG_TRIVIAL etc. appear to be macros since they’re in all caps. Who
> knows what functions they actually call?
> * The “<<“ operator expands [unmangles] to some complex function name which
> is very compiler-dependent.
> * It’s very probable that the “<<“ operator is defined as an inline
> function, so it may not even appear in the binary of the compiled Boost
> library at all.
> * The C string constants on the right hand side are probably being
> implicitly converted to C++ std::strings before the call. Or maybe not. And
> the conversion likely happens on the caller’s side, i.e. the compiler has to
> know to create a std::string before calling and then destruct it
> The only practical way to call a C++ API from another language is to write
> glue code, a set of functions that call the library, but which have a plain
> C API and use only C types. Pretty much any language has a way to call C
> functions, so you can now call the C API which will call the C++ code.
Ethin D. Probst
More information about the swift-users