为什么编译后的机器代码(EXE,PE,APP)不能在所有平台上运行? [英] Why can't compiled machine code (EXE, PE, APP) work on all platforms?

查看:137
本文介绍了为什么编译后的机器代码(EXE,PE,APP)不能在所有平台上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(据我所知)将C代码编译为exe/pe/app后,它将转换为机器代码.然后可以由处理器运行它.

When C code is compiled to an exe/pe/app, (from my knowledge) it is converted into machine code. This can then be run by a processor.

我的问题是,由于这是非常低的级别,因此它不应调用任何特定于OS的功能(因为这些功能也已经被编译为机器代码了).那么为什么不能在Linux,Windows,OSX等不同平台上运行它呢?

My question is, since this is very low level, it shouldn't make any calls to OS specific functions (as these will already have been compiled to machine code as well). So why can't it be run on different platforms, like Linux, Windows, OSX?

推荐答案

问题的前提是基于对计算机工作原理的误解.

The premise of the question is based on a big misunderstanding about how computers work.

编译一个简单的"hello world"可执行文件.拆解它,或

Compile a simple "hello world" executable. Disassemble it, or let the Godbolt Compiler Explorer do that for you.

它是否包含puts/printf的库实现的副本?否.它与libc 动态链接,因此每个程序不需要每个库函数的自己的副本它使用.

Does it contain a copy of the library implementation of puts / printf? No. It's dynamically linked to libc so every program doesn't need its own copy of every library function it uses.

它是否包含实际在视频内存中绘制文本的图形驱动程序?不,当然不是,对于在 multi-具有内存保护功能的任务操作系统:操作系统不能让进程直接访问硬件;他们将能够使计算机崩溃或在彼此的窗口上绘制.

Does it contain graphics drivers that actually draw the text in video memory? No, of course not, and that wouldn't even be possible for a program that runs in a multi-tasking OS with memory protection: The OS can't let processes access the hardware directly; they'd be able to crash the computer or draw on each other's windows.

相反,进程进行系统调用以与自身外部的事物进行交互.

Instead, processes make system calls to interact with things outside of themselves.

撇开所有这些,有多种架构无法理解彼此的机器代码.因此,即使在同一OS中,x86二进制文件也不会在ARM CPU上本地运行.

Leaving all that aside, there are multiple architectures that don't understand each other's machine code. So even within the same OS, an x86 binary won't run natively on an ARM CPU.

这篇关于为什么编译后的机器代码(EXE,PE,APP)不能在所有平台上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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