Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a program written in C++ work?

When we run a java program if the JRE is not installed, it does not work.
I found out that most of the famous applications like Google chrome browser has been written in C++. So how does windows run a program like that without any run time environment for C++?What is really happening at the installation?

like image 878
Assasins Avatar asked Sep 04 '12 05:09

Assasins


People also ask

How a program works in C?

1) C program (source code) is sent to preprocessor first. The preprocessor is responsible to convert preprocessor directives into their respective values. The preprocessor generates an expanded source code. 2) Expanded source code is sent to compiler which compiles the code and converts it into assembly code.

How code is compiled in C?

It is done with the help of the compiler. The compiler checks the source code for the syntactical or structural errors, and if the source code is error-free, then it generates the object code. The c compilation process converts the source code taken as input into the object code or machine code.


1 Answers

So how does windows run a program like that without any run time environment for C++?

The premise of the question is actually not true. At least on Windows, there is in fact a runtime environment for C++. One component of this runtime (probably the most important one) is called the C Runtime, or the CRT. :-)

Typically before your program even enters the main() function, the CRT performs a bunch of initialization routines, and when you return from the main() function it cleans up after itself. The whole point of this dance is to provide standard functionality that virtually all C and C++ programs require.

If you've ever run across an error involving a missing msvcrt.dll or anything like that (e.g. msvcr110.dll for newer programs) when starting a Windows program, the lack of the CRT is what the program is complaining about. The msvcrt.dll is the file that implements the CRT. It stands for "Microsoft Visual C Runtime".

Obviously, msvcrt.dll and its relatives ship with the Windows operating system, which is why you don't typically run into problems with a missing runtime environment unlike the JRE, which must be installed by either the user or by the manufacturer of the computer.

However, Windows C++ applications are compiled to use a specific version of the MSVCRT, and if you have the wrong version of the MSVCRT, then the operating system will complain the same way as though it was missing.* What installers typically do is to check that the OS has the correct version, and if it doesn't it copies it somewhere on your computer from its own installation files.

The MSVCRT is however not a necessary nor sufficient condition for all Windows programs to work. It's entirely possible to write a program that is not dependent on the MSVCRT, and also entirely possible that a Windows program will have dependencies other than the MSVCRT. Virtually all nontrivial Windows programs will depend on the MSVCRT and other operating system components. The installer of the program would check for these as well.

There are some important differences between the JRE and the MSVCRT. One big difference is that the JRE implements a virtual machine environment for Java applications (that's how it achieves it's "cross-platform" capabilities) which may involve just-in-time compilation, etc. while the MSVCRT only provides standard functions and does nothing about the assembly code of your C++ programs.


*This is not strictly correct as C++ applications can statically link to the MSVCRT, which doesn't depend on the DLL. However, most Windows C++ applications dynamically link to it, in which case the correct DLL is required.

like image 75
In silico Avatar answered Sep 28 '22 18:09

In silico