简体   繁体   中英

can a library contain multiple bindings

if i have written a library in C++, and have bindings for C, Ada, Fortran, D & other compiled languages.

could all these bindings be in the same binary with the C++ compiled code even if they use the same function names?

and could you use the bindings like this?

You can link C++ with C, provided you are only calling C style functions (outside of objects) and have turned name mangling off in the header via "extern C". Especially if you are using the same compiler. Different compilers will cause problems if they use different obj formats. I don't know about ADA/Fortran/D, but I suspect the answer will be no, at least directly via .LIB or .OBJ files. On windows, you can try via DLL's if ADA/FORTRAN/D supports dynamic linking (or you can call the windows api LoadLibrary).

This is not an easy thing to do and I glossed over the details. If you are really going to try, then you'll need to be specific about which platforms and compilers you are using and I'll try to be more specific.

Depending how you create your bindings a library may not be even necessary:

  • Bindings written some interpreter specific APIs:

For example, ruby extensions written using the MRI API, are basically a shared library providing a:

void init_Modulename()

This function then uses the MRI api like rb_define_module, rb_define_class, rb_define_method, etc to wrap the C/C++ APIs. Make sure this function is surrounded by extern "C" so it's name does not get mangled in the C++ way.

Normally this shared library goes linked against the library you are binding, but nothing prevents that they are the same shared library.

  • Bindings done at runtime

For example bindings using FFI on Ruby and other interpreters. The bindings get defined in the same language and it is the FFI library that knows how to call the methods in the target library at runtime. Therefore in this case the bindings themselves have no "library".

  • Bindings done with generators

If you use a generator, like SWIG, it will scan the library headers and generate the bindings for various languages. Depending on how those targets are implemented by the SWIG generator (for example, for Ruby uses the MRI API described above) then SWIG will create code you can compile into its own library, but as long as you don't have symbol conflicts, you could as well compile this together with your library.

When I mean symbol conflicts, I don't mean the API itself, but the binding helpers, like init_Modulename().

Yes. An example (slightly reversed) is PlPlot ; it's written in C and has bindings to Ada, C++, D, Fortran77, Fortran95, Java, Lua, OCaml, Python, ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM