了解外来功能界面(FFI)和语言绑定 [英] Understand foreign function interface (FFI) and language binding

查看:110
本文介绍了了解外来功能界面(FFI)和语言绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

长期以来,我不太了解如何混合不同的编程语言.根据这篇Wikipedia文章,可以使用多种方法来完成外部功能接口(或FFI)方式:

Mixing different programming languages has long been something I don't quite understand. According to this Wikipedia article, a foreign function interface (or FFI) can be done in several ways:

  1. 要求以特定方式指定或实现将要被宿主语言调用的来宾语言功能;通常使用某种兼容库.
  2. 使用工具通过适当的粘合代码自动包装"来宾语言功能,从而执行任何必要的翻译.
  3. 包装器库的使用
  4. 限制可以跨语言使用的宿主语言功能集.例如,从C调用的C ++函数通常可能不包含引用参数或引发异常.
  1. Requiring that guest-language functions which are to be host-language callable be specified or implemented in a particular way; often using a compatibility library of some sort.
  2. Use of a tool to automatically "wrap" guest-language functions with appropriate glue code, which performs any necessary translation.
  3. Use of wrapper libraries
  4. Restricting the set of host language capabilities which can be used cross-language. For example, C++ functions called from C may not (in general) include reference parameters or throw exceptions.

我的问题:

  1. 第一种,第二种和第三种方式?看来 我,他们都是为了编译代码 所谓的语言变成一些 具有目标文件和标头的库 文件,然后由 呼叫语言.
  2. 链接的一个来源说, 实施FFI可以在 几种方法:

  1. What are the differences between the 1st, 2nd and 3rd ways? It seems to me they are all to compile the code of the called language into some library with object files and header files, which are then called by the calling language.
  2. One source it links says, implementing an FFI can be done in several ways:

  • 要求目标语言中的被调用函数实现一个 特定协议.
  • 实现采用给定低语言的包装器库 函数,然后将其与代码包装"在一起,以执行与/之间的数据转换 高级语言约定.
  • 要求声明为原生的函数使用高级功能的子集(与低级语言兼容).
  • Requiring the called functions in the target language implement a specific protocol.
  • Implementing a wrapper library that takes a given low-language function, and "wraps" it with code to do data conversion to/from the high-level language conventions.
  • Requiring functions declared native to use a subset of high-level functionality (which is compatible with the low-level language).

我想知道是否第一个方法 链接的来源与 维基百科的第一种方式?

I was wondering if the first way in the linked source is the same as the first way in Wikipedia?

这第三种方式是什么 来源是什么意思?它对应于Wikipedia中的第四种方式吗?

What does the third way in this source mean? Does it corresponds to the 4th way in Wikipedia?

语言绑定和FFI 等价的概念?他们怎么样 相关和不同?

Are Language binding and FFI equivalent concepts? How are they related and differ?

编程语言的绑定 API到库或OS服务 在 语言.

a binding from a programming language to a library or OS service is an API providing that service in the language.

  • 我想知道以下每个示例在Wikipedia或引文中的引用方式是哪一种?

  • I was wondering which way in the quotation from Wikipedia or from the source each of the following examples belongs to?

    • Common Object Request Broker Architecture (CORBA)
    • Calling C in C++, by the extern "C" declaration in C++ to disable name mangling.
    • Calling C in Matlab, by MATLAB Interface to Shared Libraries, i.e., first compiling C code to shared library via general C compiler such as gcc, and then loading, calling a function from and unloading the shared library via Matlab functions loadlibrary(), calllib() and unloadlibrary().
    • Calling C in Matlab, by Creating C/C++ Language MEX-Files
    • Calling Matlab in C, by mcc compiler
    • Calling C++ in Java, by JNI, and Calling Java in C++, also by JNI
    • Calling C/C++ in other languages, Using SWIG
    • Calling C in Python, by Ctypes module.
    • Cython
    • Calling R in Python, by RPy
    • Programming Language Bindings to OpenGL from various languages, such as Python, Fortran and Java
    • Bindings for a C library, such as Cairo, from various languages, such as C++, Python, Java, Common Lisp

    感谢您的启发!最好的问候!

    Thanks for your enlightenment! Best regards!

    推荐答案

    可能有一个特定的示例会有所帮助.让我们以宿主语言为Python,来宾语言为C.这意味着Python将调用C函数.

    May be a specific example will help. Let us take the host language as Python and the guest language as C. This means that Python will be calling C functions.

    1. 第一个选择是以特定方式编写C库.对于Python,标准方法是在其他条件下使用第一个参数Py_Object *编写C函数.例如(从此处):

    1. The first option is to write the C library in a particular way. In the case of Python the standard way would be to have the C function written with a first parameter of Py_Object * among other conditions. For example (from here):

    static PyObject *
    spam_system(PyObject *self, PyObject *args)
    {
        const char *command;
        int sts;
    
        if (!PyArg_ParseTuple(args, "s", &command))
            return NULL;
        sts = system(command);
        return Py_BuildValue("i", sts);
    }
    

    是可从Python调用的C函数.为此,必须在编写库时考虑Python兼容性.

    is a C function callable from Python. For this to work the library has to be written with Python compatibility in mind.

    如果要使用现有的C库,则需要另一个选项.一种是具有一种工具,该工具以适合于宿主语言使用的格式来生成此现有库的包装.以 Swig 为例,该语言可用于绑定多种语言.给定现有的C库,您可以使用swig有效地生成C代码,该代码在遵循Python约定的同时调用现有的库.有关构建Python模块的信息,请参见示例.

    If you want to use an already existing C library, you need another option. One is to have a tool that generates wraps this existing library in a format suitable for consumption by the host language. Take Swig which can be used to tie many languages. Given an existing C library you can use swig to effectively generate C code that calls your existing library while conforming to Python conventions. See the example for building a Python module.

    我们已经存在的C库的另一个选择是从Python库调用它,该库在运行时有效地包装了调用,例如

    Another option to us an already existing C library is to call it from a Python library that effectively wraps the calls at run time, like ctypes. While in option 2 compilation was necessary, it is not this time.

    另一件事是,有很多选项(确实有重叠),可以用一种语言从另一种语言调用函数.有FFI(据我所知等效于语言绑定)通常指的是在同一个进程(可以说是同一可执行文件的一部分)中的多种语言之间的调用,并且存在进程间通信方式(本地和网络).诸如CORBA和Web服务(SOAP或REST)以及COM +和远程过程调用之类的东西通常属于第二类,不被视为FFI.实际上,他们大多没有规定在交流的任何一方都可以使用的任何特定语言.我会松散地将它们作为IPC(进程间通信)选项,尽管在基于网络的APi(如CORBA和SOAP)的情况下这是简化的.

    Another thing is that there are a lot of options (which do overlap) for calling functions in one language from another language. There are FFIs (equivalent to language bindings as far as I know) which usually refer to calling between multiple languages in the same process (as part of the same executable, so to speak), and there are interprocess communication means (local and network). Things like CORBA and Web Services (SOAP or REST) and and COM+ and remote procedure calls in general are of the second category and are not seen as FFI. In fact, they mostly don't prescribe any particular language to be used at either side of the communication. I would loosely put them as IPC (interprocess communication) options, though this is simplification in the case of network based APi like CORBA and SOAP.

    在您的清单上,我会提出以下意见:

    Having a go at your list, I would venture the following opinions:

    • 公共对象请求代理体系结构: IPC,不是FFI
    • 通过C ++中的extern "C"声明在C ++中调用C以禁用名称重整. ****
    • 通过MATLAB接口在共享库中调用Matlab中的C 选项3(类似ctypes)
    • 通过创建C/C ++语言MEX文件在Matlab中调用C 选项2(类似swig)
    • 通过mcc编译器在C中调用Matlab 选项2(类似swig)
    • 通过JNI调用Java中的C ++,通过JNI调用C ++中的Java 选项3(类似ctypes)
    • 使用SWIG 选项2(swig)
    • 以其他语言调用C/C ++
    • 通过Ctypes在Python中调用C 选项3(ctypes)
    • Cython 选项2(类似辫子)
    • 通过RPy调用Python中的R,选项3(类似于ctypes),部分与数据交换(非FFI)有关
    • Common Object Request Broker Architecture: IPC, not FFI
    • Calling C in C++, by the extern "C" declaration in C++ to disable name mangling. ****
    • Calling C in Matlab, by MATLAB Interface to Shared Libraries Option 3 (ctypes-like)
    • Calling C in Matlab, by Creating C/C++ Language MEX-Files Option 2 (swig-like)
    • Calling Matlab in C, by mcc compiler Option 2 (swig-like)
    • Calling C++ in Java, by JNI, and Calling Java in C++ by JNI Option 3 (ctypes-like)
    • Calling C/C++ in other languages, Using SWIG Option 2 (swig)
    • Calling C in Python, by Ctypes Option 3 (ctypes)
    • Cython Option 2 (swig-like)
    • Calling R in Python, by RPy Option 3 (ctypes-like) in part, and partly about data exchange (not FFI)

    接下来的两个根本不是外来函数接口,因为使用了该术语. FFi与编程语言之间的交互有关,应该能够使一种语言的任何库(具有适当的限制)可供另一种语言使用.可以使用一种语言访问的特定库不是FFI品牌.

    The next two are not foreign function interfaces at all, as the term is used. FFi is about the interaction between to programming languages and should be capable of making any library (with suitable restrictions) from one language available to the other. A particular library being accessible from one language does not an FFI make.

    • 编程从各种语言到OpenGL的语言绑定
    • 各种语言对C库的绑定

    这篇关于了解外来功能界面(FFI)和语言绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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