Linux可执行文件是否与OS X兼容?

时间:2022-07-12 02:38:44

If you compile a program in say, C, on a Linux based platform, then port it to use the MacOS libraries, will it work?

如果你在基于Linux的平台上用C语言编译一个程序,然后将它移植到MacOS库中,它会工作吗?

Is the core machine-code that comes from a compiler compatible on both Mac and Linux?

来自编译器的核心机器代码在Mac和Linux上都兼容吗?

The reason I ask this is because both are "UNIX based" so I would think this is true, but I'm not really sure.

我问这个问题的原因是因为它们都是基于“UNIX”的,所以我认为这是正确的,但我不是很确定。

5 个解决方案

#1


28  

No, Linux and Mac OS X binaries are not cross-compatible.

不,Linux和Mac OS X二进制文件不是相互兼容的。

For one thing, Linux executables use a format called ELF.

首先,Linux可执行文件使用一种叫做ELF的格式。

Mac OS X executables use Mach-O format.

Mac OS X可执行文件使用Mach-O格式。

Thus, even if a lot of the libraries ordinarily compile separately on each system, they would not be portable in binary format.

因此,即使许多库通常在每个系统上单独编译,它们也不能以二进制格式移植。

Furthermore, Linux is not actually UNIX-based. It does share a number of common features and tools with UNIX, but a lot of that has to do with computing standards like POSIX.

此外,Linux实际上并不是基于unix的。它确实与UNIX共享许多共同的特性和工具,但其中很多都与POSIX之类的计算标准有关。

EDIT:

编辑:

Finally, to address your point on byte-code: when making a binary, compilers usually generate machine code that is specific to the platform you're developing on. (This isn't always the case, but it usually is.)

最后,要解决字节码的问题:在生成二进制代码时,编译器通常生成特定于正在开发的平台的机器代码。(情况并非总是如此,但通常是如此。)

#2


4  

In general you can easily port a program across various Unix brands. However you need (at least) to recompile it on each platform.

通常,您可以轻松地跨各种Unix品牌移植程序。但是,您至少需要(至少)在每个平台上重新编译它。

Executables (binaries) are not usable on several platforms, because an executable is tightly coupled with the operating system's ABI (Application Binary Interface), i.e. the conventions of how an application communicates with the operating system.

可执行程序(二进制文件)不能在多个平台上使用,因为可执行文件与操作系统的ABI(应用程序二进制接口)紧密耦合,即应用程序与操作系统通信的惯例。

For instance if your program prints a string onto the console using the POSIX write call, the ABI specifies:

例如,如果您的程序使用POSIX写调用将字符串打印到控制台,ABI将指定:

  • How a system call is done (Linux used to call the 0x80 software interrupt on x86, now it uses the specific sysenter instruction)
  • 系统调用是如何完成的(Linux过去在x86上调用0x80软件中断,现在它使用特定的sysenter指令)
  • The system call number
  • 系统调用号
  • How are the function's arguments transmitted to the system
  • 函数的参数是如何传输到系统的
  • Any kind of alignment
  • 任何类型的对齐
  • ...

And this varies a lot across operating systems.

这在操作系统中有很大的不同。

Note however that in some cases there may be “ABI adapters” allowing to run binaries of one OS onto another OS. For instance Wine allows you to run Windows executables on various Unix flavors, NDISwrapper allows you to use Windows network drivers on Linux.

不过请注意,在某些情况下,可能会有“ABI适配器”,允许将一个操作系统的二进制文件运行到另一个操作系统上。例如Wine允许您在各种Unix版本上运行Windows可执行程序,NDISwrapper允许您在Linux上使用Windows网络驱动程序。

#3


2  

"bytecode" usually refers to code executed by a virtual machine (e.g. for java or python). C is compiled to machine code, which the CPU can execute directly. Machine language is hardware-specific so it it would be the same under any OS running on an intel chip (even under Windows), but the details of how the machine code is wrapped into an executable file, and how it is integrated with system calls and dynamically linked libraries are different from system to system.

“字节码”通常指由虚拟机(例如java或python)执行的代码。C被编译成机器代码,CPU可以直接执行。机器语言是特定于硬件的所以它是相同的在任何操作系统上运行一个英特尔芯片(即使在Windows),但机器代码是如何包装的细节到一个可执行文件,和它是如何集成和系统调用动态链接库和系统不同。

So no, you can't take compiled code and use it in a different OS. (However, there are "cross-compilers" that run on one OS but generate code that will run on another OS).

不,你不能把编译后的代码用在不同的操作系统中。(然而,有“交叉编译器”在一个操作系统上运行,但生成将在另一个操作系统上运行的代码)。

#4


1  

There is no "core byte-code that comes from a compiler". There is only machine code.

没有“来自编译器的核心字节码”。只有机器代码。

While the same machine instructions may be applicable under several operating systems (as long as they're run on the same hardware), there is much more to a hosted executable than that, and since a compiled and linked native executable for Linux has very different runtime and library requirements from one on BSD or Darwin, you won't be able to run one binary on the other system.

在相同的机器指令可能适用的几种操作系统(只要它们在同一硬件上运行),还有很多托管可执行,由于编译并链接本地可执行文件为Linux有非常不同的运行时和库需求从一个BSD或达尔文,你无法在其他系统上运行一个二进制文件。

By contrast, Windows binaries can sometimes be executed under Linux, because Linux provides both a binary format loader for Windows's PE format, as well as an extensive API implementation (Wine). In principle this idea can be used on other platforms as well, but I'm not aware of anyone having written this for Linux<->Darwin. If you already have the source code, and it compiles in Linux, then you have a good chance of it also compiling under MacOS (modulo UI components, of course).

相比之下,Windows二进制文件有时可以在Linux下执行,因为Linux既为Windows的PE格式提供了二进制格式加载程序,也提供了广泛的API实现(Wine)。原则上,这个想法也可以在其他平台上使用,但是我不知道有谁为Linux<->Darwin写过这篇文章。如果您已经有了源代码,并且它是在Linux中编译的,那么很有可能它也在MacOS(当然是模块化UI组件)下编译。

#5


1  

Well, maybe... but most probably not.

好吧,也许……但很可能不是。

But if it does, it's not "because both are UNIX" it's because:

但如果是的话,那不是“因为两者都是UNIX”,而是因为:

  • Mac computers happen to use the same processor nowadays (this was very different in the past)
  • Mac电脑现在恰好使用相同的处理器(这在过去是非常不同的)
  • You happen to use a program that has no dependency on any library at all (very unlikely)
  • 您碰巧使用的程序完全不依赖于任何库(非常不可能)
  • You happen to use the same runtime libraries
  • 您碰巧使用了相同的运行时库
  • You happen to use a loader/binary format that is compatible with both.
  • 您碰巧使用了与这两种格式兼容的加载程序/二进制格式。

#1


28  

No, Linux and Mac OS X binaries are not cross-compatible.

不,Linux和Mac OS X二进制文件不是相互兼容的。

For one thing, Linux executables use a format called ELF.

首先,Linux可执行文件使用一种叫做ELF的格式。

Mac OS X executables use Mach-O format.

Mac OS X可执行文件使用Mach-O格式。

Thus, even if a lot of the libraries ordinarily compile separately on each system, they would not be portable in binary format.

因此,即使许多库通常在每个系统上单独编译,它们也不能以二进制格式移植。

Furthermore, Linux is not actually UNIX-based. It does share a number of common features and tools with UNIX, but a lot of that has to do with computing standards like POSIX.

此外,Linux实际上并不是基于unix的。它确实与UNIX共享许多共同的特性和工具,但其中很多都与POSIX之类的计算标准有关。

EDIT:

编辑:

Finally, to address your point on byte-code: when making a binary, compilers usually generate machine code that is specific to the platform you're developing on. (This isn't always the case, but it usually is.)

最后,要解决字节码的问题:在生成二进制代码时,编译器通常生成特定于正在开发的平台的机器代码。(情况并非总是如此,但通常是如此。)

#2


4  

In general you can easily port a program across various Unix brands. However you need (at least) to recompile it on each platform.

通常,您可以轻松地跨各种Unix品牌移植程序。但是,您至少需要(至少)在每个平台上重新编译它。

Executables (binaries) are not usable on several platforms, because an executable is tightly coupled with the operating system's ABI (Application Binary Interface), i.e. the conventions of how an application communicates with the operating system.

可执行程序(二进制文件)不能在多个平台上使用,因为可执行文件与操作系统的ABI(应用程序二进制接口)紧密耦合,即应用程序与操作系统通信的惯例。

For instance if your program prints a string onto the console using the POSIX write call, the ABI specifies:

例如,如果您的程序使用POSIX写调用将字符串打印到控制台,ABI将指定:

  • How a system call is done (Linux used to call the 0x80 software interrupt on x86, now it uses the specific sysenter instruction)
  • 系统调用是如何完成的(Linux过去在x86上调用0x80软件中断,现在它使用特定的sysenter指令)
  • The system call number
  • 系统调用号
  • How are the function's arguments transmitted to the system
  • 函数的参数是如何传输到系统的
  • Any kind of alignment
  • 任何类型的对齐
  • ...

And this varies a lot across operating systems.

这在操作系统中有很大的不同。

Note however that in some cases there may be “ABI adapters” allowing to run binaries of one OS onto another OS. For instance Wine allows you to run Windows executables on various Unix flavors, NDISwrapper allows you to use Windows network drivers on Linux.

不过请注意,在某些情况下,可能会有“ABI适配器”,允许将一个操作系统的二进制文件运行到另一个操作系统上。例如Wine允许您在各种Unix版本上运行Windows可执行程序,NDISwrapper允许您在Linux上使用Windows网络驱动程序。

#3


2  

"bytecode" usually refers to code executed by a virtual machine (e.g. for java or python). C is compiled to machine code, which the CPU can execute directly. Machine language is hardware-specific so it it would be the same under any OS running on an intel chip (even under Windows), but the details of how the machine code is wrapped into an executable file, and how it is integrated with system calls and dynamically linked libraries are different from system to system.

“字节码”通常指由虚拟机(例如java或python)执行的代码。C被编译成机器代码,CPU可以直接执行。机器语言是特定于硬件的所以它是相同的在任何操作系统上运行一个英特尔芯片(即使在Windows),但机器代码是如何包装的细节到一个可执行文件,和它是如何集成和系统调用动态链接库和系统不同。

So no, you can't take compiled code and use it in a different OS. (However, there are "cross-compilers" that run on one OS but generate code that will run on another OS).

不,你不能把编译后的代码用在不同的操作系统中。(然而,有“交叉编译器”在一个操作系统上运行,但生成将在另一个操作系统上运行的代码)。

#4


1  

There is no "core byte-code that comes from a compiler". There is only machine code.

没有“来自编译器的核心字节码”。只有机器代码。

While the same machine instructions may be applicable under several operating systems (as long as they're run on the same hardware), there is much more to a hosted executable than that, and since a compiled and linked native executable for Linux has very different runtime and library requirements from one on BSD or Darwin, you won't be able to run one binary on the other system.

在相同的机器指令可能适用的几种操作系统(只要它们在同一硬件上运行),还有很多托管可执行,由于编译并链接本地可执行文件为Linux有非常不同的运行时和库需求从一个BSD或达尔文,你无法在其他系统上运行一个二进制文件。

By contrast, Windows binaries can sometimes be executed under Linux, because Linux provides both a binary format loader for Windows's PE format, as well as an extensive API implementation (Wine). In principle this idea can be used on other platforms as well, but I'm not aware of anyone having written this for Linux<->Darwin. If you already have the source code, and it compiles in Linux, then you have a good chance of it also compiling under MacOS (modulo UI components, of course).

相比之下,Windows二进制文件有时可以在Linux下执行,因为Linux既为Windows的PE格式提供了二进制格式加载程序,也提供了广泛的API实现(Wine)。原则上,这个想法也可以在其他平台上使用,但是我不知道有谁为Linux<->Darwin写过这篇文章。如果您已经有了源代码,并且它是在Linux中编译的,那么很有可能它也在MacOS(当然是模块化UI组件)下编译。

#5


1  

Well, maybe... but most probably not.

好吧,也许……但很可能不是。

But if it does, it's not "because both are UNIX" it's because:

但如果是的话,那不是“因为两者都是UNIX”,而是因为:

  • Mac computers happen to use the same processor nowadays (this was very different in the past)
  • Mac电脑现在恰好使用相同的处理器(这在过去是非常不同的)
  • You happen to use a program that has no dependency on any library at all (very unlikely)
  • 您碰巧使用的程序完全不依赖于任何库(非常不可能)
  • You happen to use the same runtime libraries
  • 您碰巧使用了相同的运行时库
  • You happen to use a loader/binary format that is compatible with both.
  • 您碰巧使用了与这两种格式兼容的加载程序/二进制格式。