Question

Is it possible to write a C++ program that, say, passes an int array to a java program without writing it to a file?

Motivation for question: I was wondering what matlab is and how it came to be an application that was written in several different languages (as opposed to being available in different languages i.e., there is a version of it in C++, Java, etc... that contains the same features).

Was it helpful?

Solution

Yes. The general answer is that they have some kind of shared binary interface. Typically the de facto C API. To oversimplify it a bit, this means writing a function in language A that calls a function written in C, and that function written in C then calls a function written in language B. Some useful examples:

  • Lua is a scripting language designed to be embedded within C code. The most famous use case is a video game with the engine written in C but all the high-level stuff scripted in Lua.

  • Some languages (including Java, Python, and way more) contain a special FFI (foreign function interface) library for the sole purpose of calling C functions.

  • As others have pointed out, almost all IPC (interprocess communication) mechanisms and network communication protocols are language independent. They all revolve around sending an arbitrary series of bytes, leaving it up to the recipient to make sense of those bytes.

I'm failing to find any definitive information, but random people on the internet seem to think that MATLAB's UI is written in Java and uses IPC to talk to the interpreter written in C++.

OTHER TIPS

Yes. Each process can communicate with another using a thing called IPC, or Inter Process Communication (unsurprisingly!). There are many different forms of IPC, file IO being the one used by a fair few unix programs, but you can use sockets, named pipes or memory-maps, or even a database (using something along the lines of the blackboard architectural pattern).

The language used to create a process is irrelevant (except for its support to access the same IPC mechanism).

Nearly all languages that are worth writing in have some way of interfacing with a C dynamic library. In java that is done with the Java Native Interface or by hosting the JVM in memory like a scripting engine of sorts.

With matlab there is a similar setup to java's.

From C you would call Engine engine = engopen(NULL); and use engine to send commands to the matlab process using either a COM service or a a set of pipes.

Licensed under: CC-BY-SA with attribution
scroll top