wood burning stoves*
The moose likes General Computing and the fly likes C++ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » General Computing
Bookmark "C++" Watch "C++" New topic
Author

C++

Abhijeet Pathak
Ranch Hand

Joined: Jul 05, 2009
Posts: 33
How exactly C,C++ are platform dependent languages?
Can we run a .exe file without C++ compiler?


I am the one who knows that I don't know anything.
Abhijeet
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11444
    
  16

First and foremost, the term 'platform independant' is NOT well defined. It means different things to different people, so discussing/arguing whether something IS or ISN'T makes no sense unless you define what YOU mean by it.

And yes, you can run a .exe without a compiler. However, you cannot CREATE an .exe without a compiler (and linker).


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

In continuation to what fred said, Java is "compile once, run anywhere" which is not true for C and C++.
Its not only OS dependent,but at times its also hardware dependent so something build for Intel hardware might not work on AMD.

SCJP, SCWCD.
|Asking Good Questions|
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
A lot of C/C++ code will run on an AMD/PPC/i386/64-bit Pentium, but has to be compiled anew for each computer.
Abhijeet Pathak
Ranch Hand

Joined: Jul 05, 2009
Posts: 33
fred rosenberger wrote:First and foremost, the term 'platform independant' is NOT well defined. It means different things to different people, so discussing/arguing whether something IS or ISN'T makes no sense unless you define what YOU mean by it.

And yes, you can run a .exe without a compiler. However, you cannot CREATE an .exe without a compiler (and linker).


So if can run a .exe without a compiler then can't we write a code on one machine, compile it and create an exe file
then we can copy that .exe file to other machine (which does not has a c++ compiler) and run that file.
Abhijeet Pathak
Ranch Hand

Joined: Jul 05, 2009
Posts: 33
Amit Ghorpade wrote:In continuation to what fred said, Java is "compile once, run anywhere" which is not true for C and C++.
Its not only OS dependent,but at times its also hardware dependent so something build for Intel hardware might not work on AMD.


So does that mean every time we want to run a C++ program we have to compile it?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
No (more-or-less) to both your last questions.

You can compile a C/C++ program into a file, and copy that to another computer and run it, but it will probably only work if the two computers are running the same operating system and same sort of hardware. If you compile it on a Pentium on Windows it will probably run on another Pentium/Windows combination. Actually a lot of this software is designed for i386 or i586 chips which were the forerunner of Pentiums. It will probably not work on a PPC running a MacOS, and might not work on a Pentium running Linux (but I have never tried).
Once you have compiled and run a program on any computer, you can run the same compiled file again, as long as no new errors or changes have been applied.

Maneesh, Fred, Please translate that into English for Abhijeet
vamsi acharya
Ranch Hand

Joined: Jun 28, 2009
Posts: 55

From what i have learned C has been a big hit for its portability in their contemporary world ........then what does that mean ???


"Known is a drop , unknown is an ocean " !
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3720
    
  16
vamsi acharya wrote:From what i have learned C has been a big hit for its portability in their contemporary world ........then what does that mean ???


This means you could compile some source code into an executable using a Windows X86 compiler and run it successfully on a Windows X86 machine.
You could then compile the same source code into an executable using a Solaris/Sparc compiler and run that executable on a Solaris/Sparc machine.

However, the Windows/X86 executable will not run on the Solaris/Sparc machine and the Solaris/Sparc executable will not run on the Windows/X86 machine.

ie. it is the source code that is portable, not the executables. Having said that however, once you get into stuff like networking, C code becomes a lot less portable (without the use of a lot of #ifdefs).

With Java, you can compile the source code into class files and then those class files can be run on any machine with a JVM. You would not need a different version of the class files for each machine type.


Joanne
vamsi acharya
Ranch Hand

Joined: Jun 28, 2009
Posts: 55

Joanne Neal wrote:
vamsi acharya wrote:From what i have learned C has been a big hit for its portability in their contemporary world ........then what does that mean ???


This means you could compile some source code into an executable using a Windows X86 compiler and run it successfully on a Windows X86 machine.
You could then compile the same source code into an executable using a Solaris/Sparc compiler and run that executable on a Solaris/Sparc machine.

However, the Windows/X86 executable will not run on the Solaris/Sparc machine and the Solaris/Sparc executable will not run on the Windows/X86 machine.

ie. it is the source code that is portable, not the executables. Having said that however, once you get into stuff like networking, C code becomes a lot less portable (without the use of a lot of #ifdefs).

With Java, you can compile the source code into class files and then those class files can be run on any machine with a JVM. You would not need a different version of the class files for each machine type.


Well java is platform independent but JVM is not .....similarly C code as you said is portable but not it's executables .....pardon me but sounds the same for me !!!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11444
    
  16

Look at it from a distribution point of view. You write a program in C++. You have to compile your code into an .exe file for every OS you want to run on. you have an .exe for Windows, a different .exe for Unix, a different .exe for Macs, etc., and for each and every version of the OS. You may have dozens of .exe files. You make one minor change to your source code, and you have to re-compile each and every one. You have to pay to license at least some of those compilers (and maintain and update your compilers).

A customer calls and says they need a bug fixed. You need to make sure you give them the exact right version of the .exe for their version of their OS.

I write my code in Java. I compile it once. I have ONE set of class files. I can put them on a file server for my customers and let them download updates. I don't have to worry about them getting the RIGHT version, since EVERYONE gets the same files.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11444
    
  16

put it another way... Code written in C++ and code written in Java can both run on many different machines/OS once they are compiled. But the difference is in exactly WHERE you convert the pieces to the final form that the hardware can run directly. For C, it works like this:

1) Write code in C language

2) compile into code for a specific OS version

3) run that code on that machine.


it's that #2 that messes up C/C++. You have to to that for each and ever OS you want to run your program on.

java works like this:

1) write code in java

2) convert to bytecode - Java's own special language - these are class files

3) convert bytecode to machine's native language - this has to be done for each and every OS you want you program to run on

4) run that native language code


What's cool here is that people have taken care of that #3 for you. Almost every major OS has had someone write that 'interpreter'. You pass the interpreter your bytecode, and it can make it into a specific machine's native language. Since each and every interpreter takes Java bytecode as input, the bytecode becomes 'platform independant'.

vamsi acharya
Ranch Hand

Joined: Jun 28, 2009
Posts: 55

yes thank you, but how can we exactly differentiate the terms 'Portability' and 'platform independent' ??
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14278
    
  21

"Portability" usually means that the program can be compiled on other platforms without too much effort - which means that the programmer has not used lots of libraries which are tied to one specific operating system.

"Platform independent", at least in the case of Java, means that the program does not even have to be recompiled at all. You can just run the program on any operating system for which there is a Java runtime environment available.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11444
    
  16

and not to complicate the matter, but a java program CAN be written to end up being non-platform independent. if you use something like the System.exec() (or whatever it's called... i've never used it) to execute some specific program like 'grep' on a Unix box, that program may not exist on a windows box, and so your program would fail.

Now, it would start up, but it would crash somewhere in the middle.

No unix compiled C++ program would even come close to starting on a window box.
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
If the programmer hard-coded specific file-separator characters in the progam, the Java program would not be "platform independent." It would never find any files

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: C++