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

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

问题描述

如何语言绑定工作?

例如如何将一个来自写在一种语言到另一种语言的库做出的绑定?将绑定写在相同的语言库或语言绑定呢?

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?

推荐答案

在大多数情况下多数语言那里要么用C写的(Perl,Python和Ruby的,TCL ...)或者是用C(C ++兼容, C#,Objective-C的)。因此,对于大多数语言很容易写一些包装功能的数据结构转换以该语言为母语的C数据结构使用C库。甚至还有这样的自动(或半自动取决于所需的复杂性)工具: SWIG

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.

这是主要的原因,大多数图书馆都写在C.它一位刚刚便于口水平低code到多个目标语言。使用这种策略库的例子包括SQLite的,传统知识和wxWidgets的。

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.

另一个策略是使用操作系统功能的库导出到一个语言中立共享库。在Windows中,这将是DLL文件和Unixen他们会被共享库。大多数Microsoft产品使用这种策略,因此并不重要原code写的,你可以很容易,只要进入图书馆,因为它被编译为一个DLL。使用这种策略非Microsoft库的例子包括libpurple和GTK。

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.

第三个选择是使用IPC。最常见的方法是使用插座,因为它是最熟悉的人,很跨平台。使用此方法的code是不严格来说,图书馆。它们是服务器和它们的API是技术上的服务。但要使用这些服务一般的程序员,他们看起来像普通的API,因为大多数语言绑定抽象掉网络code和present简单的函数/方法调用。 库使用这种策略的例子包括Xwindows下,瘸子脚本和大多数数据库如MySQL和Oracle。

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.

有其他的,提供接入写在另一种语言,包括实际上嵌入该语言的跨preTER但上述3是最常见的库更令人费解的方式。

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.

我觉得我应该澄清的第一和第二种方法的差异之间的位。

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

在第一种方法中,该库仍然编译成一个DLL或者是.so象第二种方法,但主要区别是,该DLL必须符合更高级别的标准/协议。因为它需要所有的值进入和出来的函数是指向一个结构Tcl_Obj TCL为例无法加载任意DLL。因此,为了使用编译成一个普通的老DLL你需要编译通过该转换所有变量和函数参数为结构Tcl_Obj *

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*.

但是,像VB一些语言可以装入普通旧的C的DLL。因此,这将是第二种方法的一个例子。

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

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

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