是C ++平台依赖吗? [英] Is C++ platform dependent?

查看:234
本文介绍了是C ++平台依赖吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以说C ++是平台相关的吗?

Can we say that C++ is platform dependent?

我知道C ++使用编译器,并且这些编译器对于不同的平台是不同的。当我们使用编译器编译C ++代码时,例如:在Windows上,创建 .EXE 格式文件。

I know that C++ uses compiler, and those compiler are different for different platforms. When we compile C++ code using compiler for example: on Windows, .EXE format file created.

为什么是文件操作系统/平台相关?

Why is an .EXE file OS/Platform dependent?

.EXE文件中的格式是什么?

为什么我们不能在其他平台上运行它?

Why can't we run it on other platforms?

推荐答案

为简单起见,它归结为两件事:操作系统和CPU架构。

This is actually a relatively extensive topic. For simplicity, it comes down to two things: operating system and CPU architecture.

首先,* .exe通常只有Windows,因为它是代码,Windows操作系统知道如何执行。此外,操作系统知道如何将这转换为架构的正确代码(这就是为什么Windows只是兼容)。注意,还有更多的事情在进行,但这是一个(非常)的高层抽象。

First of all, *.exe is generally only Windows since it is binary code which the windows operating system knows how to execute. Furthermore, the operating system knows how to translate this to the proper code for the architecture (this is why Windows is "just compatible"). Note that a lot more is going on, but this is a (very) high-level abstraction of what is going on.

编译器将获取C ++代码并为体系结构(即x86,MIPS等)生成其对应的汇编代码。通常编译器也有一个汇编器(或者它可以依赖的汇编器)。汇编器链接代码并生成硬件可以执行的二进制代码。有关此主题的详情,请查看有关热电联产的详情。

Now, compilers will take C++ code and generate its corresponding assembly code for the architecture (i.e. x86, MIPS, etc.). Usually the compiler also has an assembler (or one which it can rely on). The assembler the links code and generates binary code which the hardware can execute. For more information on this topic look for more information on co-generation.

其他注意

考虑与平台无关的Java。 Java编译器生成在Java虚拟机(JVM)上运行的Java字节码。重要的是要注意,任何时候你希望运行一个Java应用程序,你必须运行Java虚拟机。由于预编译的JVM知道如何对您的操作系统和CPU架构进行操作,因此它可以运行其Java字节码,并为您的特定系统有效地运行相应的操作。

Consider Java which is not platform-dependent. Java compilers generate Java bytecode which is run on the Java virtual machine (JVM). It is important to notice that any time you wish to run a Java application you must run the Java virtual machine. Since the precompiled JVM knows how to operate on your operating system and CPU architecture, it can run its Java bytecode and effectively run the corresponding actions for your particular system.

编译的二进制文件(即一个来自C ++代码),你有系统字节码。因此,Java模拟的指令类型直接硬编码到.exe或您使用的任何二进制格式。考虑下面的例子:

In a compiled binary file (i.e. one from C++ code), you have system bytecode. So the kind of instructions which Java simulates for you are directly hard-coded into the .exe or whatever binary format you are using. Consider the following example:

请注意,这个java代码最终必须在JVM中运行,不能独立运行。

Notice that this java code must eventually be run in the JVM and cannot stand-alone.

Java Code:
System.out.println("hello") (To be compiled)

Compiled Java bytecode:
Print "hello" (To be run in JVM)

JVM:
(... some translation, maybe to architecture code - I forget exactly ...)
system_print_code "hello" (JVM translation to CPU specific)

与C ++模式):

C++ Code:
cout<< "hello";

Architecture Code:
some_assembly_routine "hello"

Binary output:
system_print_code "hello"

这篇关于是C ++平台依赖吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆