简体   繁体   中英

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?

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 (eg 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.

The question seems obvious, but if we dig a bit it appears far from obvious. Let me give you a more abstract view:

Every language can be "executed" by a suitable machine that wires the language istruction to a specific piece of hardware the "does" the operation.The difference is how the "wiring" is done: how much direct it is and when it is set up.

A windows (but the same works for linux as well) machine is at a minimum a set of functions exposed by certain "vital" DLL (Kernel.dll for example) to be called by the processor machine code.

C programs are typically translated at compile time into machine code that place system calls respecting that "protocol". A program that does not require any other functionality can be executed nativelly in that environment.

A program that requires other functionalities can:

  • Link statically to the library that implement the functions into machine code (in fact incorporating their code): and this makes the program still natively executable.
  • Link dynamically to those libraries: the program need to load and link those library at startup or during execution: if those library are themselves already part of the operating system the program just runs, otherwise, those libraries have to be placed somewhere in a way the program can find them (it is the case of the C runtime-support, for version other than the one used to compile the OS itself)

A java program, by the way the Java language was designed and implemented, does not translate directly into machine code. It translate into an intermediate language (the byte-code) that must be used as "input" for an interpeter (the java machine) that actually execute its instruction by acting in machine code onto the underlying machine.

Since Windows itself is not written in Java, it doesn't need such an interpreter for itself, hence you cannot find it directly on a windows installation, but you have to place it there if you need it.

to clarify that, a java program is compiled to bytecode via jdk and then runs in the jre

a c++ programm is directly compiled to machine readable code, just like the jre has to be compiled to run in windows

Runtime (or C or C++ standard libraries) are typcally installed with operating system (glibc, msvcrt, ...).

C/C++ programs can also be "statically" compiled, where library parts that are used by program are linked (merged) into an executable, so program has libraries in it's own binary file.

As for "Virtual machine" that executes Java code. C++ is usually compiled into native machine code of system it will be run on. CPU is that non-virtual machine that will run it.

It is also possible to compile C++ programs into various kinds of "bytecode" (.NET CLR, LLVM), but this is less common.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM