Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can programs which are written in c++ run anywhere?

Tags:

I know that for running c++ on Windows you need to compile specifically for Windows and the same goes for linux and on...

But for example, if I'm compiling program for Windows written in native c++ I can run it on a freshly installed windows pc? I mean, without downloading visual c++ runtime libraries, etc, I can just compile it, let's say, reinstall windows on my computer, and run it without installing anything else?

(The question above using Windows as an example but the same thing can be done on freshly installed linux distro? e.g Ubuntu) Thanks in advance.

like image 806
UnTraDe Avatar asked Feb 04 '13 20:02

UnTraDe


People also ask

Can C code run anywhere?

Yes, the standard libraries are everywhere. Just think about it, most of your programs that you get are writen in C/C++. Only dependencies come when you use some specified libraries like winsock etc, therefore some windows applications are unlikely to work on linux and vice versa.

Does C++ run on all platforms?

When it's installed, you can use C++ to create code that runs on iOS and Android devices and platforms, Windows, Windows Store, and Xbox. Writing code for multiple platforms is often frustrating. The primary development languages and tools for iOS, Android, and Windows are different on each platform.

What platforms can C++ run on?

C++ runs on lots of platform like Windows, Linux, Unix, Mac, etc. Before we start programming with C++. We will need an environment to be set-up on our local computer to compile and run our C++ programs successfully.

Can a C++ program run C?

All C++ compilers also support C linkage, for some compatible C compiler. When you need to access a function compiled with C linkage (for example, a function compiled by the C compiler, or a function written in assembler), declare the function to have C linkage.


2 Answers

The only answer is "it depends".

There are many ways that an OS can "run" a program, many ways a program can be build, and many way code can be assembled.

A program that uses only "standard libraries" and that links all libraries statically, does not need any other dependency (in the sense that all the code it need is in the binary itself or into OS libraries that -being part of the system itself- are already on the system).

But:

  • statically link the standard libraries (which are most likely present in all programs) will bloat the memory usage of many copy of the same code. That's the reason library are often linked dynamically, but this requires "installation" of those libraries as well

  • Programs that use only standard libraries can do only the things that are somehow "common" (or can be commonly represented) into all systems, thus loosing all the peculiarity that makes an OS different from another.

  • There are "platforms" that - by the nature of their peripherals - are not represented one into the other: a coffee machine has 12 keys and a textual 2 row x 20 col display. A PC has a mouse, a keyboard, and a display that can reach even 10'000 pixel of width, of millions of color each. A tablet has a touch surface that can seize multiple points at the same time. Can you imagine a program running the same on all those three platform?

like image 150
Emilio Garavaglia Avatar answered Sep 21 '22 14:09

Emilio Garavaglia


No. Often the libraries are different on different systems. If your program involves any GUI then you will definatly have OS specific code that won't run on other OSs.

If you write a C++ program targeting the g++ compiler without GUI code there may still be some OS specific code. But you should be able to port it with minimal effort.

If your program only uses stl and stdio, then it will probably be portable. For example, MS STL's ::c_str() function works a little different than the linux one.

like image 44
Tony Velito Avatar answered Sep 19 '22 14:09

Tony Velito