Is a Linux executable "compatible" with OS X?

bgroenks picture bgroenks · Feb 25, 2012 · Viewed 12.4k times · Source

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.

Answer

Tom picture Tom · Feb 25, 2012

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.

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