4

At the end of the compilation process, the program is in a .exe file in machine code. So shouldn't the machine be able to run it without having to install something like MS Visual Studio C++? Basically, I am making a program with mingw and want to share it with someone else. I do not understand why I can not just send them the .exe file. Clarification will be appreciated.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
ICanKindOfCode
  • 1,000
  • 1
  • 11
  • 32
  • Read it https://stackoverflow.com/questions/16167305/why-does-my-application-require-visual-c-redistributable-package – 273K Feb 24 '18 at 13:56
  • @S.M.Then how do I run this .exe on another computer? most only seems to be for visual studio. – ICanKindOfCode Feb 24 '18 at 13:58
  • mingw runtime is required if any by the same reasons. – 273K Feb 24 '18 at 14:00
  • The other computer may have the runtime already (it probably has). Note that the runtime isn't the same as having Visual Studio installed - it's a separate thing. It just so happens that you get these runtimes installed when Visual Studio is installed. – Robinson Feb 24 '18 at 14:02
  • You don't like mingw, consider not using it. VS can build you a program without dependencies: http://www.davidlenihan.com/2008/01/choosing_the_correct_cc_runtim.html – Hans Passant Feb 24 '18 at 14:05
  • @Hans Passant " VS can build you a program without dependencies" - No it can *not* (except in the most trivial case - as can any compiler). Programs compiled by Visual Studios compiler depend on its runtime library as well. Some versions of this are shipped as part of windows, bu *not* all. In general, you need to ship the redistributable runtime as part of your application. – Jesper Juhl Feb 24 '18 at 14:17
  • http://wixtoolset.org/documentation/manual/v3/howtos/redistributables_and_install_checks – Jesper Juhl Feb 24 '18 at 14:29
  • @JesperJuhl: Hans's link shows how to switch to statically linking said runtime. Is that article inaccurate? – Lightness Races in Orbit Feb 24 '18 at 14:50
  • @Lightness Races in Orbit not that I know of, but there exist many situations where static linking is not possible. – Jesper Juhl Feb 24 '18 at 15:41

2 Answers2

11

C++ compiles your code to machine code. If your program is self-contained, that is all you need. However, more complex running programs often relies on additional compiled code, which is made available to your program through a library.

Generally, libraries come in two "flavors" - static and dynamic. Static libraries are "baked into" your compiled code. This is not ideal, because multiple programs include identical code, leading to code duplication. Dynamic libraries, on the other hand, are shared among all programs using them, leading to more efficient use of space.

Installing runtime adds dynamic libraries for use by all programs compiled with C++.

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • So it is basically that we are using the built-in libraries, which have to be provided as DLLs by the runtimes? – ICanKindOfCode Feb 24 '18 at 14:05
  • 6
    *"If your program is as simple as `cout << "Hello, world!"` that is all you need"* bad example, iostream depends on quite a lot of stuff that is generally provided by the runtime library. – Matteo Italia Feb 24 '18 at 14:06
  • 2
    `cout` depends on a lot though. – Hatted Rooster Feb 24 '18 at 14:07
  • @MatteoItalia You are right, even the simplest `"hello, world"` program may require libraries. Thank you! – Sergey Kalinichenko Feb 24 '18 at 14:08
  • 5
    Note that the runtime is not just the library functions you might call, but also bits of library code that the compiler assumes will be available. Your program doesn't really start at `main`. The compiler generates code that initializes things and the calls `main`. Some of that generated code may rely on the runtime, even if you never call a library function. – Adrian McCarthy Feb 24 '18 at 14:20
5

Your program likely calls many functions from the standard library that you didn't write yourself. You need the runtime libraries for that. Your code probably also needs code run before main to setup the basic environment that's expected for a C++ program - the runtime libs do that for you. Also after main ends, various cleanup needs to happen according to the standard (and your program probably also depends on this) and the compilers runtime libraries take care of this.

Your code does not exist in a vacuum (it can, but then it's no longer a standard hosted C++ program). It depends on and relies on the standard runtime libs to provide the environment the C++ standard says you can expect.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
  • So if I compiled with g++(mingw), will I be able to run it on a machine with visual studio redistributable installed? Since it is just a runtime? – ICanKindOfCode Feb 24 '18 at 14:03
  • @KidDoesCodingAnd HasNoFriends - No. Then you would need to distribute mingw's runtime libs with your program. Just like a program compiled with visual studio would require you to distribute that compilers runtime libs. The runtime libraries needed by your program depends on the compiler used to build it. – Jesper Juhl Feb 24 '18 at 14:05
  • But won't they be compiled to the same .exe files? Or am I missing something? – ICanKindOfCode Feb 24 '18 at 14:07
  • 3
    Only if you link them statically, if not, you still need the mingw's runtime libs alongside them. – Hatted Rooster Feb 24 '18 at 14:08
  • 3
    @KidDoesCodingAnd HasNoFriends you are missing the fact that the "exe" is not a standalone thing. It depends on other things, like the runtime libraries of the compiler used to build it. – Jesper Juhl Feb 24 '18 at 14:09
  • "but then it's no longer a standard **hosted** C++ program" – Adrian McCarthy Feb 24 '18 at 14:22
  • @Adrian McCarthy good point. I did not mention the difference between a hosted and free standing implementation since I didn't think it was relevant. But you *are* correct. – Jesper Juhl Feb 24 '18 at 14:33
  • @KidDoesCodingAndHasNoFriends: I think you're missing the gaping chasm between specification and implementation! – Lightness Races in Orbit Feb 24 '18 at 14:49