高洛峰2017-04-17 13:13:40
This is actually divided into two types, one is library calling, and the other is cross-language calling.
For library calling, you only need a standardized ABI. Linux/Windows have their own ABI specifications. , as long as it meets this specification, the compiled library can be called by other languages. A very simple example, a library written in C/Fortran language can be called by other C++ programs.
There is also a cross- Language, usually seen in Native language and Managed language. For a C++ program to interact with a Python program, FFI must be standardized at the Python layer, and then C++ can write plug-ins for Python and let Python call C++. Java/ Python and the like are like this. Essentially, the ABI of the managed language is different from that of the Native language, and it needs to be standardized on the managed language side.
For more information about ABI, see https://zh.wikipedia.org/wiki/X86%E8%B0%83%E7%94%A8%E7%BA%A6%E5%AE%9A
ringa_lee2017-04-17 13:13:40
x86_64 has a set of parameter passing specifications, and all languages follow this specification.
天蓬老师2017-04-17 13:13:40
Java and python do not generate machine code, they generate bytecode for use by their respective virtual machines.
Then the java virtual machine (commonly used Sun JVM) is written in c++, and the commonly used python (CPython) interpreter is written in c. They also provide interfaces for calling cc++ programs, jvm provides JNI, and python can Learn about ctypes
巴扎黑2017-04-17 13:13:40
There are many ways, I will list the main ones. The one in bold is the host. When starting, it starts from the host's entrance, and the following is executed through various methods:
C/C++ + C/C++: Each platform has its own dynamic library. The compiler constructs this library according to the specifications set by the operating system and exposes the entrances to each function in the library. The operating system has system calls for loading dynamic libraries.
Java + C/C++: Someone already mentioned JNI. You first use C/C++ to write an external library according to JNI regulations, and then start the Java bytecode control virtual machine to load the external library. The virtual machine is written in C++, so we go back to method 1.
Python + C/C++: ctype. It is similar to Java. The Python runtime loads external libraries and then uses them for python code. Back to method 1.
C/C++ + Python: libpython. python provides an external library libpython to help C/C++ add the function of executing python scripts. libpython parses the script code in real time and then runs it.
C/C++ + Lua: Embed. Very similar to method 4, the script code is parsed and run in real time. However, Lua can be statically linked into the executable file because it is relatively small.
C/C++ + GLSL: embedded. Similar to method 5, the code is analyzed and compiled in real time and then handed over to the GPU for running :-)
There is also a way to communicate using child processes and pipes...such as CGI :-)
In general, (1) system calls (dlopen/dlsym/LoadLibrary
) act as an intermediary to provide traffic channels for various binary codes, (2) no matter what language it is, its underlying layer (virtual machine/runtime/library ) It is definitely impossible for the binary code to be dangling. Then everyone can use their imagination to create various hybrid programming methods.
高洛峰2017-04-17 13:13:40
My knowledge is relatively short-lived. I only know how to program in mixed C and assembly. You can know that one step in the compilation process of C is to compile it into assembly language first, but the assembly language embedded in C does not require this step.
In the final analysis, what the CPU can recognize is machine code 0 and 1. No matter what language, it will ultimately use machine code to run on the CPU, so the important thing in hybrid programming is the compiler's interpretation and compilation of the language.