I dont understand how the C/C++ language is portable?
1.) What makes a program compilable on 2 different O/S's?
2.) Doesn't this depend on the O/S manufacturer? How does Microsoft do this?
3.) What determines wether a language is portable or will only work on one operating system?
There is DIFFERENT compilers but standard C should work everywhere but you will have to recompile it. For example <iostream> library can be used anywhere but <windows.h> is OS specific. But for example Assembler is different everywhere and It is really not portible. Java on the other hand can run the same apps without recompiling on different operating systems because it has a program inside of which it runs.
Each compiler is written specifically for the OS it runs on. The compiled program therefore runs only on the OS it was built on.
Standard C/C++ relates to the source code. All compilers should accept any C/C++ code that was written to the C/C++ Standard and output a working program.
Therefore, writing Standard C/C++ should compile using any compiler.
As sergent and WaltP have pointed out:
Using standard C/C++ as far as possible is the best way of ensuring code portability.
At the risk of going off topic, using other cross-platform libraries/frameworks (boost, wxWidgets, GTK, QT etc.) can also make it easier to create cross-platform applications without using non-portable code (Obviously, this would only work if the libraries you're using are available on all platforms you intend to target!)
But in cases where very specific functionality is required, you may also have to do a bit of conditional compilation.
With conditional compilation you can enclose non-portable code with conditional pre-processor statements, so non-portable/platform-specific code is only compiled when the code is compiled on a particular operating system and/or using a particular compiler.
The only downside to conditional compilation is it can make the code a little messy and hard to read/understand. Especially if you plan to support a lot of different OS'es and compilers. It can also become a bit of a maintenance burden!
e.g. conditional compilation dependent on the operating system:
// use preprocessor to determine the platform: #if defined(unix) || defined(__unix__) || defined(__unix) # define PREDEF_PLATFORM_UNIX #elif defined(_WIN32) # define PREDEF_PLATFORM_WINDOWS #endif // standard C++ headers (should be cross-platform) #include <iostream> #include <string> // ... // Other standard headers here... // ... // platform dependant #includes #if defined PREDEF_PLATFORM_UNIX #include <unistd.h> // ... // other Unix/linux specific headers // ... #elif defined PREDEF_PLATFORM_WINDOWS #include <windows.h> // ... // other Windows specific headers // ... #endif
The pre-processor can also be used to perform conditional compilation on code depending on:
- The standard version of the language (C89, C99, C++98 etc.)
- The type of compiler
- The version of the compiler
- The processor architecture (Intel x86, SPARC, MIPS, PowerPC etc)
This site lists a lot of standard macros for various OSes, architectures and compilers etc: