C + + programs are platform-dependent: the specific platform on which the C + + source program compiles, links, and generates machine instructions based on the machine instructions of the current platform , so it can run the executable on any platform that is the same as the current platform. However, it is not guaranteed that the executable files produced by C + + source programs will run correctly on all platforms because different platforms may have different machine instructions. Therefore, once the platform has been replaced, it may be necessary to modify the source program and recompile the source program for the new platform, which is related to the platform .
The root cause of the C + + program's inability to cross the platform is that the compiler for the C + + source program compiles for the platform, and different platforms have different machine instructions, so they can't cross the platform.
Third, why Java can cross-platform
C + + is not cross-platform, that is, because C + + source program is compiled on the platform, so closely related to the platform. If you have an environment where it can be backwards compatible (platform compatible) and up-to-the-go (Programs can run), then you can cross the platform. Based on this principle, Java provides a Java Runtime Environment--jre (installation JDK) on the computer's operating system. The
jre consists of a Java virtual machine, a class library, and some core files, which means that Java-written software can run as long as the platform provides a Java runtime environment.
C + +, the compiler provided by the Java language is not programmed for specific operating systems and CPU chips. Instead, the Java source program is compiled for the Java Virtual machine into "intermediate code" called bytecode, and then the Java Virtual Machine is responsible for translating bytecode into the machine code of the platform on which the virtual machine resides and let the current platform run the machine code.
How the Java language works:
A bytecode file compiled on a computer (that is, a. class file, a byte-code file is a file that has been preprocessed by a compiler, is a Java executable file exists as a binary file, but not directly executed by the system, but requires a virtual machine explanation execution . ), you can copy it directly to any computer that has a Java running environment installed. The bytecode file is interpreted by the virtual machine, i.e. the Java Virtual machine translates the bytecode into the machine code of the local computer and then gives the machine code to the local operating system to run.
Because bytecode files compiled on a computer can be copied directly to any computer that has a Java runtime installed, bytecode is actually an "intermediate code", which is why "the Java language can compile and run everywhere, which is why Java is a cross-platform." "So Java is a" semi-compiled, semi-explanatory language, "that is, the source program--and compile-and-byte code interpreter----the machine language of the corresponding platform. ”
http://blog.csdn.net/newmemory/article/details/54949817