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:
- Write your code in Language 1 (For example, C++).
- 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.
- Compile (or encapsulate) the code from steps 1 and 2 into a shared library (or DLL) which exports all the C functions.
- 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.
- 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.
9319

被折叠的 条评论
为什么被折叠?



