简体   繁体   English

用C ++编写的程序如何工作?

[英]How does a program written in C++ work?

When we run a java program if the JRE is not installed, it does not work. 如果我们运行java程序,如果没有安装JRE,它就不起作用。
I found out that most of the famous applications like Google chrome browser has been written in C++. 我发现大多数着名的应用程序,如Google Chrome浏览器都是用C ++编写的。 So how does windows run a program like that without any run time environment for C++?What is really happening at the installation? 那么Windows如何在没有任何C ++运行时环境的情况下运行这样的程序?安装过程中究竟发生了什么?

So how does windows run a program like that without any run time environment for C++? 那么Windows如何在没有任何C ++运行时环境的情况下运行这样的程序?

The premise of the question is actually not true. 这个问题的前提实际上并非如此。 At least on Windows, there is in fact a runtime environment for C++. 至少在Windows上,实际上有一个C ++的运行时环境。 One component of this runtime (probably the most important one) is called the C Runtime, or the CRT. 此运行时的一个组件(可能是最重要的组件)称为C运行时或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. 通常在程序进入main()函数之前,CRT执行一系列初始化例程,当你从main()函数返回时,它会自行清理。 The whole point of this dance is to provide standard functionality that virtually all C and C++ programs require. 这种舞蹈的重点是提供几乎所有C和C ++程序所需的标准功能。

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. 如果您在启动Windows程序时遇到过一个涉及缺少msvcrt.dll或其他类似错误的错误(例如msvcr110.dll用于较新的程序),那么缺少CRT就是程序所抱怨的。 The msvcrt.dll is the file that implements the CRT. msvcrt.dll是实现CRT的文件。 It stands for "Microsoft Visual C Runtime". 它代表“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. 显然, msvcrt.dll及其亲属附带Windows操作系统,这就是为什么你不会遇到缺少运行时环境的问题,这与JRE不同,JRE必须由用户或计算机制造商安装。 。

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. 但是,Windows C ++应用程序被编译为使用特定版本的MSVCRT,如果您的MSVCRT版本错误 ,那么操作系统会抱怨它的缺失方式。*安装程序通常做的是检查操作系统具有正确的版本,如果没有,则将其从计算机的某个位置复制到自己的安装文件中。

The MSVCRT is however not a necessary nor sufficient condition for all Windows programs to work. 但是,对于所有Windows程序而言,MSVCRT不是必要条件,也不是充分条件。 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. 编写一个不依赖于MSVCRT的程序是完全可能的,并且完全有可能Windows程序将具有除MSVCRT之外的依赖项。 Virtually all nontrivial Windows programs will depend on the MSVCRT and other operating system components. 几乎所有重要的Windows程序都依赖于MSVCRT和其他操作系统组件。 The installer of the program would check for these as well. 程序的安装程序也会检查这些。

There are some important differences between the JRE and the MSVCRT. JRE和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. 一个很大的区别是JRE实现了Java应用程序的虚拟机环境(这就是它实现了它的“跨平台”功能),这可能涉及即时编译等,而MSVCRT只提供标准功能而且什么都不做关于C ++程序的汇编代码。


*This is not strictly correct as C++ applications can statically link to the MSVCRT, which doesn't depend on the DLL. *这不是严格正确的,因为C ++应用程序可以静态链接到MSVCRT,而MSVCRT不依赖于DLL。 However, most Windows C++ applications dynamically link to it, in which case the correct DLL is required. 但是,大多数Windows C ++应用程序动态链接到它,在这种情况下需要正确的DLL。

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. Windows(但同样适用于Linux)机器至少是由处理器机器代码调用的某些“重要”DLL(例如Kernel.dll)暴露的一组函数。

C programs are typically translated at compile time into machine code that place system calls respecting that "protocol". C程序通常在编译时被翻译成机器代码,该机器代码将系统调用置于该“协议”之下。 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) 动态链接到这些库:程序需要在启动时或执行期间加载和链接这些库:如果这些库本身已经是操作系统的一部分,程序就会运行,否则,这些库必须以某种方式放置在某个地方程序可以找到它们(这是C运行时支持的情况,对于用于编译OS本身的版本以外的版本)

A java program, by the way the Java language was designed and implemented, does not translate directly into machine code. Java语言的设计和实现方式不直接转换为机器代码。 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. 它转换为中间语言(字节代码),必须用作实际执行其指令的插入程序(java机器)的“输入”,方法是将机器代码作用到底层机器上。

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. 由于Windows本身不是用Java编写的,因此它本身不需要这样的解释器,因此您无法直接在Windows安装上找到它,但如果需要,您必须将它放在那里。

to clarify that, a java program is compiled to bytecode via jdk and then runs in the jre 为了澄清这一点,java程序通过jdk编译为字节码,然后在jre中运行

a c++ programm is directly compiled to machine readable code, just like the jre has to be compiled to run in windows c ++程序直接编译为机器可读代码,就像jre必须编译为在windows中运行一样

Runtime (or C or C++ standard libraries) are typcally installed with operating system (glibc, msvcrt, ...). 运行时(或C或C ++标准库)通常安装有操作系统(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. C / C ++程序也可以“静态”编译,其中程序使用的库部件链接(合并)为可执行文件,因此程序在其自己的二进制文件中具有库。

As for "Virtual machine" that executes Java code. 至于执行Java代码的“虚拟机”。 C++ is usually compiled into native machine code of system it will be run on. C ++通常被编译为将运行的系统的本机机器代码。 CPU is that non-virtual machine that will run it. CPU是将运行它的非虚拟机。

It is also possible to compile C++ programs into various kinds of "bytecode" (.NET CLR, LLVM), but this is less common. 也可以将C ++程序编译成各种“字节码”(.NET CLR,LLVM),但这种情况不太常见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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