Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a C++ program require to run?

This question has been bothering me for a while now. Let's consider the two following programs:

#incude <iostream>
int main()
{
   std::cout << "Hello, World!";
}

and

int main()
{
   int x = 5;
   int y = x*x;
}
  1. Windows: The first example, naturally, requires some system .dll's for the console. I understand that. What about the second? Does it need anything to run? Some runtime libraries? By the way, what do runtime libraries actually do?
  2. Linux: No idea, can you enlighten me?

I know it depends on the compiler and OS, but I need either a general answer or particular examples. TIA.

like image 985
Armen Tsirunyan Avatar asked Oct 15 '10 11:10

Armen Tsirunyan


1 Answers

As a general answer, the first will require the C++ runtime libraries (the stuff you need to support the standard library calls). These form an interface of sorts between the language and the support libraries, which in turn know how to achieve what they do in the given environment.

The second makes no use of the runtime libraries. It will use the C startup and termination code (that initialises and tears down the C environment) but it's a discussion point as to whether or not these are considered part of the runtime libraries. If you consider them a part, then , yes, they will be used. It will probably be a very small part used since there's usually a big difference in size between startup code and the streams stuff.

You can link your code statically (binding at link time) with runtime libraries or dynamically (so that the actual binding is done at load time). That's true for both Windows and Linux.

like image 198
paxdiablo Avatar answered Sep 28 '22 12:09

paxdiablo