Is a Linux executable "compatible" with OS X?
Asked Answered
P

5

27

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

Is the core machine-code that comes from a compiler compatible on both Mac and 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.

Photosphere answered 24/2, 2012 at 23:18 Comment(3)
No, they are not binary compatible at all.Madonia
As others have said, binary compatibility is a very difficult task (it can be done, generally with emulation). However, source compatibility (e.g. C that uses only available cross-platform libraries/API) is a lot more attainable. Look at the MacPorts project, which makes "ports" of many "UNIX" programs (through source code) installable on OS X. There are still a bunch of different source tweaks for different targets in some cases however... and there is a whole tool ecosystem just to try to simplify this process.Learn
[Also, don't confuse can from above with feasible ;-) Consider how much code/time has been invested into Wine, and it only works in certain cases. In short an entirely different system/core library stack -- namely WinAPI -- has been ported to run in Linux.]Learn
R
36

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

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

Mac OS X executables use Mach-O format.

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.

All this said, people can and do create pretty cool ways to deal with the problem of cross-compatibility.

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.)

Romine answered 24/2, 2012 at 23:31 Comment(6)
Although it is possible for a system to support multiple executable formats... there be even more compatibility issues, however. (e.g. entirely different library stacks if the calling convention differs).Learn
So if both an Mach-O and ELF binary target amd64, is the difference mainly a 'wrapper' ?Ressieressler
The difference is the format of the executable and library files that tell the kernel how to map segments of the file into memory, resolve symbol definitions and relocations, etc.Sundaysundberg
I see. That does make sense. I could've sworn UNIX was the forefather of Linux however...Photosphere
Linux is definitely inspired in some ways by UNIX, but it started out as a hobby project of Linus Torvalds's, so it has no UNIX code in it, AFAIK.Romine
It depends on what you mean by "UNIX" (or "Unix"). The trademark is owned by The Open Group, which says it refers to any OS that has been certified to meet the Single UNIX Specification. A couple of Linux distributions have been certified, regardless of whether they share any source code with the original AT&T Unix. en.wikipedia.org/wiki/Unix#BrandingHols
S
6

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

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.

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

  • How a system call is done (Linux used to call the 0x80 software interrupt on x86, now it uses the specific sysenter instruction)
  • 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.

Shit answered 24/2, 2012 at 23:29 Comment(0)
B
4

"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.

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).

Bedroll answered 24/2, 2012 at 23:32 Comment(0)
B
2

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.

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).

Baliol answered 24/2, 2012 at 23:25 Comment(0)
H
0

Well, maybe... but most probably not.

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

  • Mac computers happen to use the same processor nowadays (this was very different in the past)
  • 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.
Holarctic answered 24/2, 2012 at 23:34 Comment(1)
Well yes, but then you might as well say "if you run it on the platform where it was compiled". The answer to OP's question is, simply, no. OS X uses Mach-O format and Linux uses ELF. Even if you statically linked an executable, and ran it on a similar processor, it would not work.Crore

© 2022 - 2024 — McMap. All rights reserved.