一个库可以包含多个绑定吗
如果我用 C++ 编写了一个库,并且具有 C、Ada、Fortran、D 和的绑定;其他编译语言。
所有这些绑定是否可以与 C++ 编译代码位于同一二进制文件中,即使它们使用相同的函数名称?
你能像这样使用绑定吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据您创建绑定的方式,库甚至可能不是必需的:
例如,使用 MRI API 编写的 ruby 扩展基本上是一个共享库,提供:
然后该函数使用 MRI api,如 rb_define_module, rb_define_class、rb_define_method 等用于包装 C/C++ API。确保该函数被 extern "C" 包围,这样它的名称就不会以 C++ 的方式被破坏。
通常,这个共享库会链接到您正在绑定的库,但没有什么可以阻止它们是同一个共享库。
例如,在 Ruby 和其他解释器上使用 FFI 进行绑定。绑定以相同的语言定义,并且 FFI 库知道如何在运行时调用目标库中的方法。因此,在这种情况下,绑定本身没有“库”。
如果您使用生成器(例如 SWIG),它将扫描库标头并生成各种语言的绑定。根据 SWIG 生成器实现这些目标的方式(例如,对于 Ruby 使用上述 MRI API),SWIG 将创建您可以编译到其自己的库中的代码,但只要不存在符号冲突,您就可以也可以将其与您的库一起编译。
当我指的是符号冲突时,我指的并不是 API 本身,而是绑定助手,例如 init_Modulename()。
Depending how you create your bindings a library may not be even necessary:
For example, ruby extensions written using the MRI API, are basically a shared library providing a:
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.
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".
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().
您可以将 C++ 与 C 链接起来,前提是您仅调用 C 样式函数(在对象之外)并通过“extern C”在标头中关闭名称修改。特别是如果您使用相同的编译器。不同的编译器使用不同的 obj 格式会导致问题。我不了解 ADA/Fortran/D,但我怀疑答案是否定的,至少直接通过 .LIB 或 .OBJ 文件。在 Windows 上,如果 ADA/FORTRAN/D 支持动态链接,您可以通过 DLL 进行尝试(或者您可以调用 Windows api LoadLibrary)。
这不是一件容易的事,我掩盖了细节。如果您真的要尝试,那么您需要具体说明您正在使用哪些平台和编译器,我会尝试更具体。
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.
是的。一个示例(稍微相反)是 PlPlot;它是用 C 编写的,并绑定到 Ada、C++、D、Fortran77、Fortran95、Java、Lua、OCaml、Python...
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, ...