Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do language bindings work?

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?

like image 328
paldepind Avatar asked Dec 28 '10 14:12

paldepind


People also ask

What is language binding in programming?

In programming and software design, binding is an application programming interface (API) that provides glue code specifically made to allow a programming language to use a foreign library or operating system service (one that is not native to that language).

What type of binding occurs at language?

Static Binding (cont) • At language definition. – Structure of language.

What are Python bindings?

If so, then Python bindings allow you to call functions and pass data from Python to C or C++, letting you take advantage of the strengths of both languages. Throughout this tutorial, you'll see an overview of some of the tools you can use to create Python bindings.

What are rust bindings?

Rust refers to all declarations as Bindings as they bind a name you create to some type of data. All bindings begin with let . let x = 1; This will bind x to the value of 1 . Rust has strong type inference so we did not have to specify what type x would be, the compiler automatically figured it out.


1 Answers

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.

like image 137
slebetman Avatar answered Oct 04 '22 03:10

slebetman