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

Asked

Viewed 458 times

15

I know how to program in C and I am now learning Java, but I would love to learn C++ and I wanted to know if it is possible to create programs cross Platform, that is, that they run in any OS without having to compile the code in each OS.

2 answers

13


No compiling for each platform is impossible. But there is practically no language more portable than C++. It is more than Java, contrary to what marketing says.

The motto of Java is "write Once, run Everywhere", even if it is a lie that runs anywhere and also doesn’t tell the whole truth because it doesn’t count that where it runs, it usually runs badly, because it uses the lowest common denominator to run in "all" places.

C++ prefers "write Once, Compile Everywhere". Of course you have to take some precautions, there is a situation that doesn’t write just 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 usually not necessary to have the target operating system to produce an executable for it. Good C and C++ compilers can produce executables for other platforms even from where they are running. Of course, this does not apply to real application tests. At the very least you will have to have a virtual machine with the desired operating system if you want to test whether everything is okay on that platform. Something that everyone should do, even when programming in Java, save very simple applications.

In good applications, which are useful in fact, compiling for various platforms is not a problem. Who chooses C++ usually wants quality and C++ gives you everything you need to deliver this quality. It requires greater effort, of course.

Practically everything that goes for C, goes for C++.

Multi-platform library

Every operating system has its peculiarities. Whenever you are doing IO (data input and output operations, includes screens, networks, etc.) you may come across different conditions. Some libraries can abstract this and help the same code be used in the various systems without or with minimal modifications. It’s the same as in Java.

But no library will do the best, they all adopt the "lowest common denominator". Some more successfully than others. Those who try to abstract the most end up creating bad products in 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 that their code has differentiated parts for each operating system, to a greater or lesser degree.

One of the most used GUI libraries that tracks various extra operating system abstractions is the Qt. It works very well on Windows, is considered native on Linux, has improved a lot in the mobile world (devices mainstream) and has been acceptable at macos.

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

  • Correct me if I’m wrong. If I want someone who has OS X to run my program created on Windows, I will have to compile my program on OS X so that my friend can use my program. It is +/- it?

  • No need, compilers can generate executable code for other platforms. Of course, if you want to test this on the other platform you need to have it installed, at least on a virtual machine. But this is also true in Java. The only way to make sure it works right on all platforms is to test on all platforms. If you have enough courage and confidence that tested on just one platform (Windows, for example) will work at all, then you don’t even need to see the face of OSX, Linux, Android, iOS, BSD, or qq other OS.

  • Ahhh ok! Now it’s clearer. Thanks for the reply was very helpful!

  • It would be worth mentioning in your reply the graphical interface, wouldn’t it? If the question only mentioned C vs C++, I wouldn’t make that comment. But as AP says that it is learning Java, one clear advantage that it will study there is not having to worry about the portability of user interfaces. Of course there are alternatives in C++ (for example, Qt), but it is worth remembering that this is a focus of additional concern (that is, external to the language), and that it also has its advantages (control of the native options exploitation/optimizations of the OS graphical manager, for example).

  • 1

    @Luizvieira blz, I’ll do it. Although I’ve practically done it :)

3

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

You can compile code from one machine to several other (cross-Compile), but you really need to compile it for each specific platform you want to use.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.