Alex Rivera | Logout

What does a C++ program require to run?

Asked 2010-10-15T11:19:55.483
15

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.

Edit
Report

4 Answers

7

For Windows applications you can use the Dependency Walker to see all dependencies.

answered 2010-10-15T11:44:49.030
2

The first program performs stream I/O, which means it has to interact with resources (console, gui) managed by the OS. So, ultimately, the OS has to be invoked via an API implemented in a system dll.

On windows the second program requires no libraries. I'm fairly sure the same is true on Linux.

answered 2010-10-15T11:26:57.527
2

Compile them with GCC, and get executable named 'hi', in console write:

ldd hi

will give you the shared objects(dynamic libraries) which are connected to your program.

Just for quick answer here is an output:

ldd tifftest
  libtiff.so.3 => /usr/lib/libtiff.so.3 (0x4001d000)
  libc.so.6 => /lib/libc.so.6 (0x40060000)
  libjpeg.so.62 => /usr/lib/libjpeg.so.62 (0x40155000)
  libz.so.1 => /usr/lib/libz.so.1 (0x40174000)
  /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
answered 2010-10-15T12:31:53.193
2

Well, let's look at this from a more general point of view:

  • To start with, you'll need a computer with a compatible CPU that works with target machine of the compiler output. You might think this is obvious but assuming that code compiles to x86 machine code, it won't run on an Alpha CPU which uses different instructions. Alternatively, if you compile to x64 machine code, it won't run on an x86-only CPU. So the correct hardware is necessary to run the C++ program, in contrast to virtual-machine based languages like Java, which abstract that away.

  • You will also need the correct operating system. I'm not an expert on porting programs but I don't think it's possible to build a single executable that runs on multiple operating systems in C++. For example, compiling even your second example to Windows will have a lot of runtime-library code behind the scenes before and after the actual call to your main() function. This will do things like prepare the heap and initialise the CRT library. The CRT for Windows is implemented via the Windows API. You can static link the library so no CRT DLL is required, but the code in your program still makes calls to the Windows API, so is still platform dependent. As an experiment I compiled an empty program with static linking on Windows with Visual Studio, and according to Dependency Walker it still references KERNEL32.DLL for functions like HeapCreate and ExitProcess. So the 'empty' program still does a whole bunch of operating system stuff for you, in preparation for doing something useful (regardless of whether or not your program does anything useful).

Also note there may well be a minimum operating system version: Visual Studio 2010 requires Windows XP SP2 or above for even an empty program, due to calls made to EncodePointer and DecodePointer. See answered 2010-10-15T16:19:47.130

Your Answer