语言绑定如何工作? [英] How do language bindings work?

查看:18
本文介绍了语言绑定如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

语言绑定是如何工作的?

例如,如何将用一种语言编写的库绑定到另一种语言?绑定会用与库相同的语言还是绑定所用的语言编写?

是否可以与所有语言进行绑定,或者该语言是否必须以某种方式支持绑定?如果是这样,那么该支持如何运作?

解决方案

在大多数情况下,大多数语言要么是用 C(Perl、Python、Ruby、Tcl ...)编写的,要么与 C 兼容(C++、C#、Objective-C).因此,对于大多数语言来说,通过编写一些包装函数来将该语言中的数据结构转换为本地 C 数据结构,可以轻松使用 C 库.甚至还有一个自动(或半自动,取决于所需的复杂性)工具:SWIG.

这是大多数库用 C 编写的主要原因之一.它只是使将低级代码移植到多种目标语言变得容易.使用此策略的库示例包括 SQLite、Tk 和 wxWidgets.

另一种策略是使用操作系统功能将库导出到语言中立的共享库中.在 Windows 上,这将是 DLL,而在 Unixen 上,它们将是共享库.大多数 Microsoft 产品都使用此策略,因此无论原始代码是用什么编写的,只要将其编译为 DLL 即可轻松访问该库.使用此策略的非 Microsoft 库的示例包括 libpurple 和 gtk.

第三个选项是使用 IPC.最常见的方法是使用套接字,因为它对大多数人来说都很熟悉并且非常跨平台.使用这种方法的代码严格来说不是库.它们是服务器,它们的API"在技术上是服务.但是对于使用服务的普通程序员来说,它们看起来像常规 API,因为大多数语言绑定抽象出网络代码并呈现简单的函数/方法调用.使用此策略的库"示例包括 Xwindows、Gimp 脚本和大多数数据库,例如 MySQL 和 Oracle.

还有其他更复杂的方法可以提供对用另一种语言编写的库的访问,包括实际嵌入该语言的解释器,但以上 3 种是最常见的.

<小时>

澄清

我觉得我应该澄清一下第一种和第二种方法的区别.

在第一种方法中,库仍然像第二种方法一样被编译成 dll 或 .so,但主要区别在于 dll 必须符合更高级别的标准/协议.例如,Tcl 不能加载任何任意的 dll,因为它期望所有进出函数的值都是指向 struct Tcl_Obj 的指针.因此,为了使用编译为普通旧 dll 的库,您需要编译另一个 dll,该 dll 通过将所有变量和函数参数转换为 struct Tcl_Obj* 的包装函数访问第一个 dll.

但是某些语言(如 VB)可以加载普通的旧 C dll.所以这将是第二种方法的一个例子.

How do language bindings work?

For instance how would one make bindings from a library written in one language to another language? Would the bindings be written in the same language as the library or the language the bindings are for?

Is it possible to make bindings to and from all languages or does the language have to somehow support bindings? If that is the case then how does that support work?

解决方案

For the most part most languages out there are either written in C (Perl, Python, Ruby, Tcl ...) or is compatible with C (C++, C#, Objective-C). Therefore, for most languages it is easy to use a C library by writing some wrapper functions to convert data structures in that language into native C data structures. There is even an automatic (or semi-automatic depending on complexity required) tool for this: SWIG.

This is one of the main reason most libraries are written in C. It just makes it easy to port the low level code to multiple target languages. Examples of libraries using this strategy include SQLite, Tk and wxWidgets.

Another strategy is to use OS features to export the library into a language-neutral shared library. On Windows this would be DLLs and on Unixen they'd be shared libraries. Most Microsoft products use this strategy so it doesn't matter what the original code is written in you can easily access the library as long as it is compiled as a DLL. Examples of non-Microsoft libraries using this strategy include libpurple and gtk.

The third option is to use IPC. The most common method is to use sockets because it's familiar to most people and very cross platform. Code that use this method are not, strictly speaking, libraries. They are servers and their "API" are technically services. But to the average programmer using the services they look like regular APIs because most language bindings abstract away the network code and present simple function/method calls. Examples of "libraries" using this strategy include Xwindows, Gimp scripting and most databases such as MySQL and Oracle.

There are other, more convoluted ways of providing access to libraries written in another language including actually embedding that language's interpreter but the above 3 are the most common.


Clarification

I feel I should clarify a bit between the difference of the first and second approach.

In the first approach, the library is still compiled into a dll or .so like the second approach but the main difference is that the dll must conform to a higher level standard/protocol. Tcl for example cannot load any arbitrary dll because it expects all values going into and coming out of a function to be a pointer to a struct Tcl_Obj. So in order to use a library compiled as a plain old dll you'd need to compile another dll that accesses the first dll via wrapper functions that convert all variables and function parameters into struct Tcl_Obj*.

But some languages like VB can load plain old C dlls. So that would be an example of the second approach.

这篇关于语言绑定如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆