Programming Languages, compilers, and portability
Posted on 2008-06-17
Maybe it's a lame question but.... why a program written with a compiled language (such as C++) must be compiled specifically for the Operating System on which it will run ?
if I'm not wrong, the compiler converts the source code to the executable which is the machine code. So I guess, when we run an executable, the operating system gives the machine code to the hardware (i.e. CPU) to be executed (correct me if this is not the case). So why that "machine code" cannot be executed both in windows and Linux ?
In the case of Java, I know that the source code is compiled to bytecode, that runs on top of the Java Virtual Machine. As soon as you have the right virtual machine for your platform, you can run any Java "compiled" program which was compiled for that specific virtual machine, regardless of the operating system on which it was compiled.
With this rationale, why a C++ executable (which is suppose to be "machine code") can't be executed on Linux, if it was compiled in windows, or vice versa ?