If C++ is machine dependent, then how does software written in C++ work on different computers?
Photoshop is written in C++, yet it runs on many computers, but C++ is not cross platform.
If C++ is machine dependent, then how does software written in C++ work on different computers?
Photoshop is written in C++, yet it runs on many computers, but C++ is not cross platform.
2
Answers
As James Adikson said an executable is compiled for each platform. But you may wonder what about handling platform specifics.
For starters a Windows executable file is internally different than a Linux one (Using Windows and Linux as examples). That is handled by having different compilers or specifying different target platforms to the compiler.
Then you have interaction with the OS, for example in Windows your file paths use ” as separator while in linux or unix they use ‘/’, these kind of things are usually handled by using
#ifdef
preprocessor directives with macros defined by the compiler. Something like:Notice that this is conditional compilation so it won’t define the same variable two times.
Also, some things are made platform independent in the standard library. File interaction is different on each system but you can create a file and append to it in a platform-independent way using the standard library.
This will compile with a Linux C++ compiler or a Windows C++ compiler and the resulting executables will communicate with their respective host OSes appropriately.
A few more examples of platform independency: