C可执行文件可以在所有操作系统上运行吗 [英] Can a C executable file run on all operating systems

查看:212
本文介绍了C可执行文件可以在所有操作系统上运行吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在操作系统(例如Windows)中创建C可执行文件,则
可以在其他操作系统(例如Linux,Mac-OS)上运行该文件。

If I create a C executable file in an operating system (ex: Windows), can this file run on other operating systems (as: Linux, Mac-OS).

推荐答案

尝试给出一个完整的答案,理论上是是,而实际上是大 但是。

Trying to give a complete answer, it would be a theoretical "yes" with a really big "but".

首先要了解的是:由C编译器创建的二进制可执行文件在与所有其他语言的编译器创建的二进制文件相关的任何内容中都没有不同。因此,以下内容与C无关。

The first thing to understand: a binary executable created by a C compiler doesn't differ in anything relevant from one created by a compiler for some other language. So the following doesn't have to do anything with C.

有两个问题。操作系统具有不同的文件格式来存储二进制文件(通常由代码,数据等部分组成,以及一些告诉系统将它们放置在何处,如何进行重定位的元信息)。然后,操作系统还以某种方式(对于Windows,是一组系统DLL)将其API公开给二进制文件。

There are two issues about that. Operating systems have different file formats for storing the binary (which typically consists of sections for code, data, ... as well as some meta-information telling the system where to place them, what relocations to do, and so on). And then, operating systems also expose their API to binaries in some way (for Windows, a set of system DLLs).

实际上,只要处理器体系结构是同样,您可以仍然在另一个系统上运行二进制文件。您只需要一些软件即可:a。)知道如何加载Windows二进制文件; b。)将win32 API调用转换为目标平台的API调用。这就是葡萄酒的作用。

Indeed, as long as the processor architecture is the same, you could still run the binary on another system. You would just need some software that a.) knows how to load a windows binary and b.) translates win32 API calls to API calls of your target platform. This is btw what wine does.

我只是猜想您来自哪里,是在阅读有关C独立于平台的信息。的确如此,但仅适用于源代码。只要您有可用的C编译器,仅使用标准C的C程序就可以在任何平台上编译(然后运行)。

I just guess where you're coming from is having read about C being platform independent. This is true, but just applies to source code. A C program using nothing but standard C will compile (and THEN run) on any platform as long as you have a C compiler for it available.

这篇关于C可执行文件可以在所有操作系统上运行吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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