How exactly is portability of a language like C is determined? I've learned that compilers are ISA specific. If this is true, how is C portable? Or is it that just the source code written in C is portable but not the executables? Aren't the executables ISA specific for examples applications for x86 are separate from the applications for Apple (assuming Apple uses Motorola/PowerPC microprocessor)?
Correct. Some people call it write once, compile everywhere. http://en.wikipedia.org/wiki/Write_once,_compile_anywhere. The other alternative is write once, run everywhere. Java is a good example of this. http://en.wikipedia.org/wiki/Write_once,_run_anywhere And even though you can achieve partial cross platform portability, you should never expect your code to run everywhere without modifications. |
|||||||
|
|
It's not just ISA specific. For example you ask:
Yes, they are, even though Apple uses x86 hardware. C binaries are architecture and operating system specific. |
|||||||||||||
|
Exactly. You need to recompile your C program on every platform. C compilers generate machine code which is portable only to a very limited extent, between machines of the same processor/memory architecture and OS. That is why you see different binary distributions of multiplatform apps (e.g. browsers), such as "Linux 64-bit Intel" or "Mac OS X 32-bit PowerPC" (OK, the last one is just an illustration, I know Apple switched to Intel a few years ago :-). |
||||
|
|
|
Most of the question has been answered but I'd like to add than durability is another thing you might have to take in account. For example, JAVA can be written once and run on any platform where the VM (today, it's called "Runtime Environment"). But another advantage is that you can run Java 1.1 code from 1995 in your 2011 machine. Which is not possible if your code was compiled on i386 and you try to run it on your AMD64 architecture. You get the improvements of the virtual machine itself as well. Then, I would say that in general, going from the least portable to the more portable languages you would have: Assembler, low level compiled language like C, then C++, then interpreted languages or the ones that run within a virtual machine. I'm not really a Java defender, at least not for the language nor the community for example, but it's the way to go if you're looking for portability and the least performance loss compared to C. |
|||
|
|
|
Good answers about write once compile anywhere. People like to think about C as a portable language because of its popularity and the high probability of a C compiler being available for future target platforms. Another factor is the standard library which helps with common programming tasks in a platform independent way. So I would say the portability of a language is determined by:
Realistically though almost any complex C application will require some work to move over to a new platform due to hardware or operating system dependencies. That process is known as porting. |
|||
|
|
|
"Portability" has multiple meanings. With respect to C, it means the following:
The source code is portable, but a new binary has to be generated for each target. Note, however, that C source is rarely "trivially" portable; most applications require you to go beyond what's defined by the language standard, using extensions that are unique to a particular platform, so in practice source code isn't 100% portable. Note, however, that C does leave quite a lot up to the implementation. The exact sizes of various data types, the behavior on overflow, etc., are all up to the implementation; the standard provides the minimum requirements that an implementation must conform to, but the implementation is free to go beyond those limits. |
|||
|
|
|
Whatever ISA is, C isn't ISA specific. I assume you're not referring to the now-obsolete slot for PC extension cards. There are standards-compliant C compilers for very many platforms, and as long as you use fully standards-defined language features in your source code, you should be able to compile it on any C compiler for any platform. However, one gotcha is that the C standard leaves a lot of behaviour of features as either implementation defined or as undefined behaviour. This is done to make the C language more generally useful for low level programming, avoiding cases where some precisely defined behaviour is a poor match for what the hardware supports on some platform. However, it does make it a little harder to write portable programs. Also, unlike some languages, C doesn't come supplied with a huge library of the kind that Java or C# provide. You can get very portable libraries to do just about anything, but you have to do some work to build them and get them to work together. C does have a standard library, of course, but its scope is relatively limited in comparison with Java, C#, Python, etc etc. |
|||||
|
