Is there a way to create cross platform programs in C ++?

14

I'm programming C and I'm learning Java now, but I really enjoyed learning C ++ and I wanted to know if it's possible to create cross platform programs, that is, to run on any OS without having to compile the code in each OS.

    
asked by anonymous 08.06.2016 / 21:39

2 answers

11

Without compiling for each platform there is no way. But there is virtually no language more portable than C ++. It is more than Java.

Java's motto is "write once, run everywhere", even though it's a lie that runs anywhere and does not tell the whole truth because it does not tell you where it runs, it usually does badly, because it uses the least common denominator run everywhere.

C ++ prefers "write once, compile everywhere". Of course you have to take some care, you have a situation that you do not only write once. But at least it runs everywhere and runs well, if the programmer knows what he's doing. C ++ does not sell illusions.

Note that it is not normally necessary to have the target operating system to produce an executable for it. Good C and C ++ compilers can produce executables for other platforms from where they are running. Of course this is not worth testing. At the very least you'll have to have a virtual machine with the desired operating system if you want to test if everything is ok on that platform. Something that everyone should do, even programming in Java, save very simple applications.

In good applications, it is actually useful to compile for multiple platforms is not a problem. Who chooses C ++ usually wants quality and C ++ gives everything it needs to deliver this quality. It requires more effort, of course.

Virtually everything that counts for C, applies to C ++.

Cross-platform library

Each operating system has its peculiarities. Whenever you are doing IO (data entry and exit operations, including screens, networks, etc.) you may encounter different conditions. Some libraries can abstract this and help the same code be used in different systems without or with minimal modifications. This is the same as in Java.

But no library will do the best, they all adopt the "least common denominator." Some more successful than others. Those who try to abstract more end up creating bad products on all operating systems, although they may eventually privilege some specific ones to the detriment of others. Those that abstract less produce better results, but require their code to have different parts for each operating system, to a greater or lesser extent.

One of the most commonly used GUI libraries that comes with several extra abstractions of operating systems is Qt . It works very well on Windows, is considered native to Linux, has greatly improved in the mobile world (mainstream ) and has acceptable results on MacOS.

Applications with less IO features, or at least more basic IO like console and files, are easier to write a single code and run on all platforms.

    
08.06.2016 / 21:46
2

There are several alternatives in C ++, allowing you to run the same source code on several platforms, but none is "automatic" without having to compile for the final platform as asked.

You can make the code compiled from one machine to another (cross-compile), but you really do need to compile for each specific platform you want to use.

    
08.06.2016 / 21:57