C++ platform dependent and Java Platform Independent

Guys, I have lot of confusion about same basic concepts. I am posting whatever I feel is right(might be entirely different from reality)
It would be great if someone could explain


We have been taught that C++ is platform dependent since .exe generated in windows can't be run in linux.
But Java is platform independent as .class file can be run in any OS as long as JVM is written for that specific OS.

JVM means Java virtual machine so isn't it like a virtual machine like VM ware?
If it is, can't we write Virtual machine for C++ and make it platform independent.


I am aware about the steps involved in running a cpp code. Conversion of a .cpp to .exe involves many steps
Does .exe has machine code?
If yes, machine code is different for every machine so how does it run?
I also learned that every compiler assumes some processor for example turbo c compiler assumes 8086 processor.
so does it create a virtual environment of 8086 processor? if yes, is that the reason why turbo c cannot access more than 1 MB of RAM?

I know lots of things take place apart from what I mentioned. It would be great if I could get an abstract view of what's happening
 
Technically, you can compile C++ programs in the same manner as Java. Compilers like LLVM can compile C++ programs to a an intermediate form using their front end (clang) and the IR code can then be compiled on the target platform using a backend compiler(LLC). So the front-end remains target independent and the backend works much like the Java's JVM.

However, this does not define a language's platform independence. Depending on which platform you're targeting, you will write your code differently. But everyone has a different interpretation for "platform independence", so choose whatever you want. By several definitions even Java is not considered a platform independent language.

As for your second question, the compiler does not assume anything. Most modern compiler are aware of the underlying architecture and are also capable of cross-compiling. I'm not sure about Turbo C, it is archaic and non-standard and I haven't seen it being used anywhere other than for academic purposes.
 
Will try to answer some of it:
We have been taught that C++ is platform dependent since .exe generated in windows can't be run in linux.
But Java is platform independent as .class file can be run in any OS as long as JVM is written for that specific OS.

JVM means Java virtual machine so isn't it like a virtual machine like VM ware?
If it is, can't we write Virtual machine for C++ and make it platform independent.
When you compile and link a C++ program it create an executable image containing code which could be natively run on the processor, whereas when you compile a java file it generates class file containing Java Byte Code which requires the JVM to convert it to machine readable format.

If you are writing a program purely using C++, you can recompile it without any changes on any platform and then run it there. The problem will arise if you are using any OS specific libraries or code inside your C++ code, in such cases you can't directly compile on other platforms.

As you can see applications written using C++/C will have a advantage over Java in term of performance (no extra overhead of JVM), also you won't be able to write system level apps using Java. There are various other pros and cons for using Java or C++, and this is generally dictated by the type of application you are trying to develop.
I am aware about the steps involved in running a cpp code. Conversion of a .cpp to .exe involves many steps
Does .exe has machine code?
Yes .exe contains code in assembly language, and so is very much dependent on the processor architecture. Apart from the code it also contains information regarding relocation, global variables, dependencies upon various libraries/dll etc..
On Windows the executable (.exe) will be structured based on the PE format ( you can read more about PE format here), and on Linux (I think ) they use ELF format. So as you can see the executable image format used varies between different OS's , this is one of the reason you could not run a exe on Linux m/c or vice-versa.
If yes, machine code is different for every machine so how does it run?
Yes machine code is different for different processor architecture, and thus could not run natively on incompatible platforms unless you have a emulator to help you out.
I also learned that every compiler assumes some processor for example turbo c compiler assumes 8086 processor.
so does it create a virtual environment of 8086 processor? if yes, is that the reason why turbo c cannot access more than 1 MB of RAM?
I think you are using a pretty old version of turbo c (most probably 16bit one), and it is time to move on to something like Visual Studio Express Edition which is free.
I know lots of things take place apart from what I mentioned. It would be great if I could get an abstract view of what's happening
 
@tifosi
@theoracle
Thanks for your reply. It cleared lot of doubts. I am using Visual Studio 2010. I said about Turbo C because our teacher gave example of Turbo C.
I thought that Turbo C also creates a Virtual Machine.
 
Last edited by a moderator:
The Java-like VM used in Android, Dalvik VM, is supposedly the cause of battery drain in Android devices...

As opposed to this, the Ubuntu Mobile OS will have native apps that will run natively without any VM(much like C++ executable format)...this will probably improve battery life....
 
Guys, I have lot of confusion about same basic concepts. I am posting whatever I feel is right(might be entirely different from reality)
It would be great if someone could explain

We have been taught that C++ is platform dependent since .exe generated in windows can't be run in linux.
But Java is platform independent as .class file can be run in any OS as long as JVM is written for that specific OS.

JVM means Java virtual machine so isn't it like a virtual machine like VM ware?
If it is, can't we write Virtual machine for C++ and make it platform independent.
yes we can write a virtual machine for c++ and make it platform independent. that's what MS achieved by creating .NET architecture where you can use almost any language (C++, C#, VB, J#, COBOL etc...) to create a platform independent program that will be run on .NET framework which is also called mono on Linux.
I am aware about the steps involved in running a cpp code. Conversion of a .cpp to .exe involves many steps
Does .exe has machine code?
If yes, machine code is different for every machine so how does it run?
the .exe contains machine code in Portable Executable format. if you compile code on a Linux machine, the binary will be created on ELF format. Now, when the compiler converts the code to machine code, it uses what is called an instruction set as a reference. instruction set is very specific to processors. labels like SSE2, SSE3 MMX, Neon extensions, ARM7 etc... specify the features in the instruction set. which means you cant execute a program compiled using Intel instruction set on a ARM chip with ARM7 instruction set. in layman terms you cant execute a binary compiled on a desktop on a raspberry pi or a Samsung phone.
I also learned that every compiler assumes some processor for example turbo c compiler assumes 8086 processor. so does it create a virtual environment of 8086 processor? if yes, is that the reason why turbo c cannot access more than 1 MB of RAM?
I know lots of things take place apart from what I mentioned. It would be great if I could get an abstract view of what's happening
The ELF format is binary independent format, which means you should be able to compile a ARM7 binary on a intel x86 machine. which is what every one does and calls it "cross compilation". similarly PE.
So, for example you want to compile a hello world program for a galaxy S3 phone you will tell the gcc that it needs to use ARM7 instruction set with hardfloat and Neon Extensions. the gcc will create a machine code that will sound like "ACBADEAD". if you tell the gcc to compile using intel x86 instruction set, then it will create machine code which will sound like "MNPONM". Also, platform independence is a generic term, there are many layers to it.
 
Back
Top