Adapter (Wrapper) for different programming language?

Most programming languages and some scripting/interpreted languages (For example: python) have the capability of calling C functions in a DLL or .so file (i.e., a shared library). This is what usually serves as the glue between any two language, and is most often the basis for other frameworks geared towards inter-language interfaces (like COM objects in Windows). If you do this by hand (without the help of a special tool or library), the process is usually as follows:

  1. Write your code in Language 1 (For example, C++).
  2. Write a set of C-compatible function (i.e., with a standard calling convention, no name-mangling, only opaque pointers or primitive types as parameters, etc.) in Language 1.
  3. Compile (or encapsulate) the code from steps 1 and 2 into a shared library (or DLL) which exports all the C functions.
  4. Write code in Language 2 that wraps the C function calls into a structure (classes, and functions) that mirror the original code (step 1), but in Language 2.
  5. Link (dynamically) all your Language 2 code with the shared library created in step 3.

For simple applications, the above can be done by hand very easily. For larger applications, it might be very difficult to do it just based on the two languages that you are dealing with (especially being able to do step 4 with fidelity). And, you would normally use an automated tool for this purpose. These tools vary between language combinations, and some consist only of libraries in the destination and/or source language, while some are more involved tools that include some parsing and code generation before you can compile. One example of a library-only tool is Boost.Python, which interfaces C++ code with Python code, both ways, and it mostly works off the fact that Python has very easy means to interface with C libraries and has a COM-like interface specification. One important example for a more elaborate tool is SWIG, which can do a lot of cross-language stuff (including things like calling a script from a C/C++ code which requires the interpreter to be launched to execute the script).

So, you see, the above steps are just the general process, but it can vary highly between languages, operating systems, and compiler suites. Some compiler suites (like ICC, GCC, MS compilers, etc.) can glue certain kinds of codes together automatically, often because they use a similar binary interface in all their compiled code. Some operating systems (mostly Linux / Unix) have standard binary interfaces and a tighter integration between shared modules and executables, which usually simplifies the process as well. So, overall, it is very much a case-by-case basis, but for a small project, the steps outlined above are a pretty safe bet.

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值