C++ – What makes a program (in a language like C++) cross-platform or not

ccross platform

I have fairly basic programming experience with Java and I've tried C++ and Python. While it makes sense for Java, the basic programs I've written in C++ have run just fine on Windows and OS X. I've been able to just send the source file to the other computer, compile, and run. The programs are fairly basic, mostly just basic object oriented stuff I've been doing to try to learn C++.

Obviously though, you can't just compile any C++ program on any machine and have it run fine. At what point does that happen? At what level of complexity does the platform start to matter and the program won't just run anywhere? Is it when you use platform specific libraries? Could a program be made cross platform in C++ just using cross platform libraries?

I've been trying to figure this out on my own but everything I've found either goes over my head or simply doesn't answer the question, much of what comes up are emulators or people asking what languages are cross platform.

Best Answer

There is a very clear line in the sand between cross-platform and mono-platform:

Does a program use only those APIs exposed by the Standard Library it is built on?

If you target a standard implementation, then any platform that implements that standard should, in theory, compile and run your program correctly. There are plenty of exceptions, but in general if you avoid clever tricks you should be able to avoid many of those exceptions.

The reason why this issue comes up specifically with C++ is that for the longest time the C++ Standard Library did not include many modules that are useful for non-trivial programs. Concurrency and GUI are the two big ones. This meant that a program built using only the C++ Standard Library could not have many of the features we expect from modern software.

How do we write cross-platform software that does stuff the Standard Library does not support?

You mention "cross platform libraries," which in practice are normally called "frameworks" or "toolkits" based on their scope. The way to write software in C++ that is cross-platform is to target not only the Standard Library, but a framework that builds on your target platforms.

Essentially, you end up externalizing the platform-specific bits to another library. For example, wxWidgets is a popular framework. You can use its classes to construct a GUI and related functionality (e.g. standard dialogs for selecting files). Under the covers, it uses conditional compiling to implement the GUI using the native GUI for each platform. But as the user of the library, none of that matters to you.


Historical note: here in 2015 C++ compilers and libraries are, for the most part, really good. Go back a few years, and that is not true. C++ did not have an official standard until 1998, many years after its inception. It took vendors a long time to update their compilers and libraries to implement the standard correctly. Vendor-specific extensions abounded (and are still around, actually). Some compilers had nonstandard headers and functionality. Code portability was virtually nonexistent. C++'s reputation in terms of portability still suffers.

Related Topic