Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are C++ applications cross-platform?

One of the first things I learned as a student was that C++ applications don't run on different operating systems. Recently, I read that Qt based C++ applications run everywhere. So, what is going on? Are C++ applications cross-platform or not?

like image 754
tony Avatar asked Oct 20 '15 13:10

tony


People also ask

Are C programs cross-platform?

The language C itself is cross-platform, because you don't directly run C code on machines. The C source code is compiled to assembly, and assembly is the platform specific code. The only non cross-platform part are the compilers and the resulting assembly.

Are C# apps cross-platform?

Yes, C# is fully cross-platform thanks to . NET Core. . NET Core is a free and open-source, managed computer software framework for Windows, Linux, and macOS operating systems.

Which apps use cross-platform?

For example, a cross-platform application may run on Microsoft Windows, Linux, and macOS. Cross-platform software may run on many platforms, or as few as two. Some frameworks for cross-platform development are Codename One, Kivy, Qt, Flutter, NativeScript, Xamarin, Phonegap, Ionic, and React Native.

Is C++ platform independent?

In case of C or C++ (language that are not platform independent), the compiler generates an .exe file which is OS dependent. When we try to run this .exe file on another OS it does not run, since it is OS dependent and hence is not compatible with the other OS.


1 Answers

  1. Source code compatible. If I compile the source code, will it run everywhere?

  2. API/ABI compatibility. Does the OS provide the interface to its components in a way that the code will understand?

  3. Binary compatibility. Is the code capable of running on the target host?

Source code compatible

C++ is a standard which defines how structures, memory, files can be read and written.

#include <iostream> int main( int argc, char ** argv ) {      std::cout << "Hello World" << std::endl; } 

Code written to process data (e.g. grep, awk, sed) is generally cross-platform.

When you want to interact with the user, modern operating systems have a GUI, these are not cross-platform, and cause code to be written for a specific platform.

Libraries such as qt or wxWidgets have implementations for multiple platforms and allow you to program for qt instead of Windows or iOS, with the result being compatible with both.

The problem with these anonymizing libraries, is they take some of the specific benefits of platform X away in the interest of uniformity across platforms.

Examples of this would be on Windows using the WaitForMultipleObjects function, which allows you to wait for different types of events to occur, or the fork function on UNIX, which allows two copies of your process to be running with significant shared state. In the UI, the forms look and behave slightly different (e.g. color-picker, maximize, minimize, the ability to track mouse outside of your window, the behaviour of gestures).

When the work you need to be done is important to you, then you may end up wanting to write platform specific code to leverage the advantages of the specific application.

The C library sqlite is broadly cross-platform code, but its low-level IO is platform specific, so it can make guarantees for database integrity (that the data is really written to disk).

So libraries such as Qt do work, they may produce results which are unsatisfactory, and you end up having to write native code.

API/ABI compatibility

Different releases of UNIX and Windows have some form of compatibility with each other. These allow a binary built for one version of the OS to run on other versions of the OS.

In UNIX the choice of your build machine defines the compatibility. The lowest OS revision you wish to support should be your build machine, and it will produce binaries compatible with subsequent minor versions until they make a breaking change (deprecate a library).

On Windows and Mac OS X, you choose an SDK which allows you to target a set of OS's with the same issues with breaking changes.

On Linux, each kernel revision is ABI incompatible with any other, and kernel modules need to be re-compiled for each kernel revision.

Binary compatibility

This is the ability of the CPU to understand the code. This is more complex than you might think, as the x64 chips, can be capable (depending on OS support) of running x86 code.

Typically a C++ program is packaged inside a container (PE executable, ELF format) which is used by the operating system to unpack the sections of code and data and to load libraries. This makes the final program have both binary (type of code) and API (format of the container) forms of incompatibilities.

Also today if you compile a x86 Windows Application (targeting Windows 7 on Visual Studio 2015), then the code may fail to execute if the processor does not have SSE2 instructions (about 10 years old CPU).

Finally when Apple changed from PowerPC to x86, they provided an emulation layer which allowed the old PowerPC code to run in an emulator on the x86 platform.

So in general binary incompatibility is a murky area. It would be possible to produce an OS which identified invalid instructions (e.g. SSE2) and in the fault, emulated the behaviour, this could be updated as new features come out, and keeps your code running, even though it is binary incompatible.

Even if your platform is incapable of running a form of instruction set, it could be emulated and behave compatibly.

like image 81
mksteve Avatar answered Sep 21 '22 14:09

mksteve