I would like to start with a THANK YOU to all those that answered my previous question on Compiling multiple languages together. Now I want to know whether or not it's possible to Compile multiple languages together so that they would run on different platforms.
views:
62answers:
3I think that whether you are compiling one or multiple languages, the answer to whether your compiled code will run on different platforms depends on the existence of a virtual machine, like the JVM (Java Virtual Machine) or CLR (Common Language Runtime) that can interpret instructions and execute them on different platforms. Since different machine architectures support different instruction sets (here an instruction set refers to assembly), it isn't possible to compile your code to machine language in such a way that it will run on different platforms. As indicated in a response to your last question, multiple languages will compile to Java bytecode (for example), which can be linked together and run on any machine that has a JVM. Because there are implementations of the JVM for many different machine architectures, your Java bytecode can be run on any of those architectures.
If you compile to machine-independent bytecode (or something similar), and then interpret the bytecodes on each target machine, it is relatively easy, as it is the same problem as compiling multiple languages to run on a single machine. Lots of people have solved this problem, to varying degrees of goodness of solution.
If you want to compile to absolute machine code, you have to include the machine code for every machine it will run on, and then the loader for each target machine has to know how to select the appropriate machine code.
Apple tried this with "fat binaries", in the days when PowerPC Macs and 68K Macs had to coexist peacefully. It worked, but it was not exactly a howling success.
Yes, it is possible to compile multiple languages together so that they work on multiple platforms. There's a few ways:
Virtual machine
Scala and Java both compile to the JVM, which runs on many platforms. Another example is .NET, which includes many languages and can be run on Windows or Linux through the Mono Project.
Fat binaries
Some file formats are fat binaries that run on several platforms. Apple has used them to transfer between hardware implementations, on two occasions.