4

Java applications can run on different platforms because they always run on top of a Virtual Machine specifically developed for each platform. So, there's no native installation, you just drop the compiled jar anywhere and run it.

Now, I'm not very familiarized with the details of C/C++ development. So I'm curious, how do C/C++ programs get ported to run natively on different platforms? What would be the general steps to:

1- Write a program with a GUI that simply displays the words "Hello World" on a window.

2- Package it so that on Windows OS, it gets installed by running an installer (.exe file).

3- Package it so that on Linux, it gets installed with the apt-get command on a console.

4- Package it so that on Mac OSX, it gets installed by dropping it on the "Applications" folder.

What library would I use to display the "Hello World" window? How would this library have been written to make it work on several platforms? Are there different versions of this library for different platforms? If so, does that mean that every time I were to package the program for a specific platform, I would have to compile it using the specific version of the library for the specific platform?

I'm just trying to get a general understanding of how this process works. It would be nice to hear some go-to tools that C/C++ developers generally use for the packaging process.

closed as too broad by gnat, Ixrec, durron597, user22815, user40980 Jun 29 '15 at 0:18

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    They take cross-platform framework and parametrized build engine. see Qt tutorials. – Basilevs Jun 27 '15 at 5:18
  • 4
    Are you naive enough to believe in the old commercial slogan about Java: "write once, run everywhere"? I don't know much about Java in practice (but read most of its specs), but I am not naive enough to believe that real-world Java applications can run on various platforms without any pain. – Basile Starynkevitch Jun 27 '15 at 7:05
  • 1
    apt-get probably don't work on Fedora, Redhat, Mageia ... linux distributions (but only on Debian & Ubuntu, etc...) – Basile Starynkevitch Jun 27 '15 at 7:22
  • 2
    A Java program which uses "c:\\user\\data.txt" as a file path won't run on POSIX systems (e.g. on Linux). And there is a well known dependency hell with Java too.... – Basile Starynkevitch Jun 27 '15 at 7:50
  • 3
    My point is then that you can write unportable Java code, and you can write portable C++ code. Portability is orthogonal to the language, it is mostly a mindset. – Basile Starynkevitch Jun 27 '15 at 9:39
12

You have two separate issues:

  • How to code a C or C++ program which can easily be ported to several operating systems. The easiest way is to use some cross-platform framework library like Qt or POCO (or perhaps libsdl or GTK) which has been ported to several platforms and provides a common set of abstractions. You could also restrict yourself to purely C99 or C++11 standard conforming code -without any external non-standardized library (but then, there are many applications that you won't be able to write, see e.g. this). Notice that the POSIX specification exists (but has nothing for GUI applications!), if you follow it, your code should be able to be compiled and run on all POSIX compliant (a.k.a. Unix standard) operating systems (e.g. Linux & MacOSX & AIX, etc..., but not Windows). Otherwise, you would use conditional compilation (#if preprocessor directive) and hopefully wrap all operating-system specific services in a few translation units (to be used by the rest of your code).

  • How to package & deploy a program (produced by compiling your C or C++ source code) on different operating systems. This is very OS specific, and even on Linux various distributions have different package managers: you'll have to do different things for Debian and for Redhat (and perhaps even different things for various versions of Linux distributions), since Debian has *.deb packages installed with apt-get or dpkg but Redhat has *.rpm packages installed with yum or rpm! However on Linux the FHS (filesystem hierarchy standard) is helpful since it defines where files of your software should go (but how they are installed is still distribution or package manager specific).

It might be actually simpler & wiser to develop and distribute your program as a free software in source form (e.g. on github), and to expect (advanced) users and sysadmins to install it by compiling its source code (then running some installation script which would copy the binary at specific places, e.g. under /usr/local/bin/ and the configuration files at other specific places, e.g. under /etc/, etc...). Eventually your free software might become packaged in most Linux distributions, and some external contributors would provide patches to enhance your code (or port it to some other operating system).

The dependency hell may always become an issue (because you might need some specific versions of specific external libraries) - and it does not depend much on the language, you could have it in Java too! -, but package managers are more or less dealing with it.

Portability (which is relative to some specification) is mostly a mindset or an ideal: A well known quote (I forgot the attributions) is "there is no portable program, there are only some programs which have been ported to some target systems" (and that is true for many programming languages, including Java).

BTW, some programs get compiled to a plain executable binary (only), other are requiring configuration files, external resources (e.g. fonts, graphic images, web or text templates, data files, script files) or dependencies. So the complexity of their deployment may vary a lot. For example, installing and deploying GNU emacs (or firefox browser) is very different -and much more complex- than installing a command line "hello world" program (which is nearly always a single executable). A graphical Qt-based "hello world" program is probably simple to install, if you assume that the target system already has the right version of Qt appropriately installed (in a usual place & configuration).

NB: notice that the C or C++ standards does not require that the implementation runs on a computer (in principle, you could unethically use a bunch of human slaves for a standard conforming C99 or C++11 implementation), and does not require a compiler. But most implementations are compiled (some few C interpreters exist however).

Not the answer you're looking for? Browse other questions tagged or ask your own question.