Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

C++

 
Abhijeet Pathak
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How exactly C,C++ are platform dependent languages?
Can we run a .exe file without C++ compiler?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48932
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48932
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
Android Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From what i have learned C has been a big hit for its portability in their contemporary world ........then what does that mean ???
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
vamsi acharya
Ranch Hand
Posts: 55
Android Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
Android Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes thank you, but how can we exactly differentiate the terms 'Portability' and 'platform independent' ??
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15277
38
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic