<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jan 13, 2017, at 8:18 AM, Ethin Probst via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><span style="font-family: Alegreya-Regular; font-size: 15px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">I was wondering how I could go about using the Boost library project</span><br style="font-family: Alegreya-Regular; font-size: 15px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Alegreya-Regular; font-size: 15px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">(among others) in swift?</span></div></blockquote><br class=""></div><div>No, it’s generally not feasible to use C++ libraries from <i class="">any</i> 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, etc.</div><div><br class=""></div><div>In the simple example you gave there are several warning signs:</div><div>* BOOST_LOG_TRIVIAL etc. appear to be macros since they’re in all caps. Who knows what functions they actually call?</div><div>* The “<<“ operator expands [unmangles] to some complex function name which is very compiler-dependent.</div><div>* 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.</div><div>* 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 afterwards.</div><div><br class=""></div><div>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.</div><div><br class=""></div><div>—Jens</div></body></html>