- Compiling For Mac On Linux Mac
- Run Linux Programs On Mac
- Compiling For Mac On Linux Virtualbox
- Linux On Mac Mini
- Linux Mac Os
- Run Mac App On Linux
Linux users who want to run Windows applications without switching operating systems have been able to do so for years with Wine, software that lets apps designed for Windows run on Unix-like systems.
Compiling For Mac On Linux Mac
There has been no robust equivalent allowing Mac applications to run on Linux, perhaps no surprise given that Windows is far and away the world's most widely used desktop operating system. A developer from Prague named Luboš Doležel is trying to change that with 'Darling,' an emulation layer for OS X.
'The aim is to achieve binary compatible support for Darwin/OS X applications on Linux, plus provide useful tools that will aid especially in application installation,' Doležel's project page states. Darwin is Apple's open source operating system, which provides some of the backend technology in OS X and iOS. The name 'Darling' combines Darwin and Linux. Darling works by 'pars[ing] executable files for the Darwin kernel... load[ing] them into the memory... and execut[ing] them.'
Ideally, I'd like to compile the matlab on the mac, move an executable over, and run on the linux machine. I have installed the appropriate MCR on the linux box, and tried 'mcc -m foo.m' and 'mbuild -glnxa64 foomain.c', which returned no errors, but the executable 'foomain' reads as a 'Mach-O executable i386' (mac) file, and won't run. @jcoffland No, the other way around. It's for building OS X applications on Linux. 'Cross-Compiling on Linux for Mac OS X 10.3 - 10.5' (emphasis added). Note that this answer is very old by now (almost 7 years old, yikes!) and the later answers below have more up to date information.
But there is a ways to go. 'Darling needs to provide an ABI-compatible [application binary interface] set of libraries and frameworks as available on OS X... by either directly mapping functions to those available on Linux, wrapping native functions to bridge the ABI incompatibility, or providing a re-implementation on top of other native APIs,' the project page notes.
Doležel, who started Darling a year ago, described the project and its progress in an e-mail interview with Ars. Darling is in the early stages, able to run numerous console applications but not much else. 'These are indeed the easiest ones to get working, albeit 'easy' is not the right word to describe the amount of work required to achieve that,' Doležel said. 'Such applications include: Midnight Commander, Bash, VIM, or Apple's GCC [GNU Compiler Collection]. I know it doesn't sound all that great, but it proves that Darling provides a solid base for further work.'
Users must compile Darling from the source code and then 'use the 'dyld' command to run an OS X executable,' Doležel said. One roadblock is actually getting Mac .dmg and .pkg application files working on a Linux system. Because doing so isn't that straightforward, Doležel said, 'I've written a FUSE module that enables users to mount .dmg files under Linux directly and without root privileges. An installer for .pkg files is underway.'
Unix/Linux synergy
The fact that OS X is a Unix operating system provides advantages in the development process. 'This saved me a lot of work,' Doležel explained. 'Instead of implementing all the 'system' APIs, it was sufficient to create simple wrappers around the ones available on Linux. I had to check every function for ABI compatibility and then test whether my wrapper works, so it wasn't as easy as it may sound.'
Another lucky break not available to Wine developers is that Apple releases some of the low-level components of OS X as open source code, 'which helped a lot with the dynamic loader and Objective-C runtime support code,' Doležel noted.
But of course, the project is an extremely difficult one. Doležel isn't the first to try it, as Darling was initially based on a separate project called 'maloader.' Doležel said he heard from another group of people 'who started a similar project before but abandoned the idea due to lack of time.'
Doležel was actually a novice to OS X development when he started Darling, being more familiar with OS X from a user's perspective than a developer's perspective. 'I have personally looked for something like Darling before, before I realized I would have to start working on it myself,' he said.
Darling relies heavily on GNUstep, an open source implementation of Apple's Cocoa API. GNUstep provides several core frameworks to Darling, and 'the answer to 'can it run this GUI app?' heavily depends on GNUstep,' Doležel said. Doležel is the only developer of Darling, using up all his spare time on the project.
No reverse-engineering
Doležel isn't reverse-engineering Apple code, noting that it could be problematic in terms of licensing and also that 'disassembling Apple's frameworks wouldn't be helpful at all because Darling and the environment it's running in is layered differently than OS X.'
The development process is a painstaking one, done one application at a time. Doležel explains:
To improve Darling, I first take or write an application I'd like to have running. If it is someone else's application, I first examine it with one of the tools that come with Darling to see what frameworks and APIs it requires. I look up the APIs that are missing in Apple's documentation; then I create stub functions for them and possibly for the rest of the framework, too. (Stub functions only print a warning when they are called but don't do any real work.)
The next step is to implement all the APIs according to the documentation and then see how the application reacts. I also add trace statements into important functions to have an insight into what's happening. I believe this is very much like what Wine developers do.
When things go wrong, I have to use GDB [GNU Debugger] to debug the original application.
It is rather unfortunate that Apple's documentation is often so poorly written; sometimes I have to experiment to figure out what the function really does. Many OS X applications seem to contain complete pieces of example code from Apple's documentation, presumably because one would have to spend a lot of time getting to understand how the APIs interact. This is why I appreciate open source so much—when the documentation is sketchy, you can always look into the code.
Years of development are needed. Similar to Wine, 'Having a list of applications known to be working is probably the best way to go,' Doležel said.
Darling should work on all Linux distributions, he said, with the catch that 'many apps for OS X are 32-bit only, and installing 32-bit packages on a 64-bit Linux system could be tricky depending on your distribution. I personally use Gentoo Linux, so I'm gradually creating a Portage overlay that would compile Darling and all dependencies for both 32-bit and 64-bit applications.'
Doležel would like to bring Angry Birds, other games, and multimedia applications to Linux. Darling could potentially 'be used to run applications compiled for iOS,' he writes on the project site. This will also be a challenge. 'The intention is to support the ARM platform on the lowest levels (the dynamic loader and the Objective-C runtime),' he writes. 'Rewriting the frameworks used on iOS is a whole different story, though.'
Introduction¶
This document will guide you in choosing the right Clang optionsfor cross-compiling your code to a different architecture. It assumes youalready know how to compile the code in question for the host architecture,and that you know how to choose additional include and library paths.
However, this document is not a 'how to' and won't help you setting yourbuild system or Makefiles, nor choosing the right CMake options, etc.Also, it does not cover all the possible options, nor does it containspecific examples for specific architectures. For a concrete example, theinstructions for cross-compiling LLVM itself may be of interest.
After reading this document, you should be familiar with the main issuesrelated to cross-compilation, and what main compiler options Clang providesfor performing cross-compilation.
Cross compilation issues¶
In GCC world, every host/target combination has its own set of binaries,headers, libraries, etc. So, it's usually simple to download a packagewith all files in, unzip to a directory and point the build system tothat compiler, that will know about its location and find all it needs towhen compiling your code.
On the other hand, Clang/LLVM is natively a cross-compiler, meaning thatone set of programs can compile to all targets by setting the -target
option. That makes it a lot easier for programmers wishing to compile todifferent platforms and architectures, and for compiler developers thatonly have to maintain one build system, and for OS distributions, thatneed only one set of main packages.
But, as is true to any cross-compiler, and given the complexity ofdifferent architectures, OS's and options, it's not always easy findingthe headers, libraries or binutils to generate target specific code.So you'll need special options to help Clang understand what targetyou're compiling to, where your tools are, etc.
Another problem is that compilers come with standard libraries only (likecompiler-rt
, libcxx
, libgcc
, libm
, etc), so you'll have tofind and make available to the build system, every other library requiredto build your software, that is specific to your target. It's not enough tohave your host's libraries installed.
Finally, not all toolchains are the same, and consequently, not every Clangoption will work magically. Some options, like --sysroot
(whicheffectively changes the logical root for headers and libraries), assumeall your binaries and libraries are in the same directory, which may nottrue when your cross-compiler was installed by the distribution's packagemanagement. So, for each specific case, you may use more than oneoption, and in most cases, you'll end up setting include paths (-I
) andlibrary paths (-L
) manually.
- be host/target specific or more flexible
- be in a single directory, or spread out across your system
- have different sets of libraries and headers by default
- need special options, which your build system won't be able to figureout by itself
General Cross-Compilation Options in Clang¶
Target Triple¶
The basic option is to define the target architecture. For that, use-target
. If you don't specify the target, CPU names won'tmatch (since Clang assumes the host triple), and the compilation willgo ahead, creating code for the host platform, which will break lateron when assembling or linking.
This document will guide you in choosing the right Clang optionsfor cross-compiling your code to a different architecture. It assumes youalready know how to compile the code in question for the host architecture,and that you know how to choose additional include and library paths.
However, this document is not a 'how to' and won't help you setting yourbuild system or Makefiles, nor choosing the right CMake options, etc.Also, it does not cover all the possible options, nor does it containspecific examples for specific architectures. For a concrete example, theinstructions for cross-compiling LLVM itself may be of interest.
After reading this document, you should be familiar with the main issuesrelated to cross-compilation, and what main compiler options Clang providesfor performing cross-compilation.
Cross compilation issues¶
In GCC world, every host/target combination has its own set of binaries,headers, libraries, etc. So, it's usually simple to download a packagewith all files in, unzip to a directory and point the build system tothat compiler, that will know about its location and find all it needs towhen compiling your code.
On the other hand, Clang/LLVM is natively a cross-compiler, meaning thatone set of programs can compile to all targets by setting the -target
option. That makes it a lot easier for programmers wishing to compile todifferent platforms and architectures, and for compiler developers thatonly have to maintain one build system, and for OS distributions, thatneed only one set of main packages.
But, as is true to any cross-compiler, and given the complexity ofdifferent architectures, OS's and options, it's not always easy findingthe headers, libraries or binutils to generate target specific code.So you'll need special options to help Clang understand what targetyou're compiling to, where your tools are, etc.
Another problem is that compilers come with standard libraries only (likecompiler-rt
, libcxx
, libgcc
, libm
, etc), so you'll have tofind and make available to the build system, every other library requiredto build your software, that is specific to your target. It's not enough tohave your host's libraries installed.
Finally, not all toolchains are the same, and consequently, not every Clangoption will work magically. Some options, like --sysroot
(whicheffectively changes the logical root for headers and libraries), assumeall your binaries and libraries are in the same directory, which may nottrue when your cross-compiler was installed by the distribution's packagemanagement. So, for each specific case, you may use more than oneoption, and in most cases, you'll end up setting include paths (-I
) andlibrary paths (-L
) manually.
- be host/target specific or more flexible
- be in a single directory, or spread out across your system
- have different sets of libraries and headers by default
- need special options, which your build system won't be able to figureout by itself
General Cross-Compilation Options in Clang¶
Target Triple¶
The basic option is to define the target architecture. For that, use-target
. If you don't specify the target, CPU names won'tmatch (since Clang assumes the host triple), and the compilation willgo ahead, creating code for the host platform, which will break lateron when assembling or linking.
---
, where:arch
=x86_64
,i386
,arm
,thumb
,mips
, etc.sub
= for ex. on ARM:v5
,v6m
,v7a
,v7m
, etc.vendor
=pc
,apple
,nvidia
,ibm
, etc.sys
=none
,linux
,win32
,darwin
,cuda
, etc.abi
=eabi
,gnu
,android
,macho
,elf
, etc.
The sub-architecture options are available for their own architectures,of course, so 'x86v7a' doesn't make sense. The vendor needs to bespecified only if there's a relevant change, for instance between PCand Apple. Most of the time it can be omitted (and Unknown)will be assumed, which sets the defaults for the specified architecture.The system name is generally the OS (linux, darwin), but could be speciallike the bare-metal 'none'.
When a parameter is not important, it can be omitted, or you canchoose unknown
and the defaults will be used. If you choose a parameterthat Clang doesn't know, like blerg
, it'll ignore and assumeunknown
, which is not always desired, so be careful.
Finally, the ABI option is something that will pick default CPU/FPU,define the specific behaviour of your code (PCS, extensions),and also choose the correct library calls, etc.
CPU, FPU, ABI¶
Once your target is specified, it's time to pick the hardware you'llbe compiling to. For every architecture, a default set of CPU/FPU/ABIwill be chosen, so you'll almost always have to change it via flags.
-mcpu=
, like x86-64, swift, cortex-a15-mfpu=
, like SSE3, NEON, controlling the FP unit available-mfloat-abi=
, like soft, hard, controlling which registersto use for floating-point
The default is normally the common denominator, so that Clang doesn'tgenerate code that breaks. But that also means you won't get the bestcode for your specific hardware, which may mean orders of magnitudeslower than you expect.
For example, if your target is arm-none-eabi
, the default CPU willbe arm7tdmi
using soft float, which is extremely slow on modern cores,whereas if your triple is armv7a-none-eabi
, it'll be Cortex-A8 withNEON, but still using soft-float, which is much better, but still notgreat.
Run Linux Programs On Mac
Toolchain Options¶
There are three main options to control access to your cross-compiler:--sysroot
, -I
, and -L
. The two last ones are well known,but they're particularly important for additional librariesand headers that are specific to your target.
There are two main ways to have a cross-compiler:
Compiling For Mac On Linux Virtualbox
When you have extracted your cross-compiler from a zip file intoa directory, you have to use
--sysroot=
. The path is theroot directory where you have unpacked your file, and Clang willlook for the directoriesbin
,lib
,include
in there.In this case, your setup should be pretty much done (if noadditional headers or libraries are needed), as Clang will findall binaries it needs (assembler, linker, etc) in there.
When you have installed via a package manager (modern Linuxdistributions have cross-compiler packages available), makesure the target triple you set is also the prefix of yourcross-compiler toolchain.
In this case, Clang will find the other binaries (assembler,linker), but not always where the target headers and librariesare. People add system-specific clues to Clang often, but asthings change, it's more likely that it won't find than theother way around.
So, here, you'll be a lot safer if you specify the include/librarydirectories manually (via
-I
and-L
).
Linux On Mac Mini
Target-Specific Libraries¶
All libraries that you compile as part of your build will becross-compiled to your target, and your build system will probablyfind them in the right place. But all dependencies that arenormally checked against (like libxml
or libz
etc) will matchagainst the host platform, not the target.
So, if the build system is not aware that you want to cross-compileyour code, it will get every dependency wrong, and your compilationwill fail during build time, not configure time.
Also, finding the libraries for your target are not as easyas for your host machine. There aren't many cross-libraries availableas packages to most OS's, so you'll have to either cross-compile themfrom source, or download the package for your target platform,extract the libraries and headers, put them in specific directoriesand add -I
and -L
pointing to them.
Linux Mac Os
Also, some libraries have different dependencies on different targets,so configuration tools to find dependencies in the host can get thelist wrong for the target platform. This means that the configurationof your build can get things wrong when setting their own librarypaths, and you'll have to augment it via additional flags (configure,Make, CMake, etc).
Multilibs¶
When you want to cross-compile to more than one configuration, forexample hard-float-ARM and soft-float-ARM, you'll have to have multiplecopies of your libraries and (possibly) headers.
Some Linux distributions have support for Multilib, which handle thatfor you in an easier way, but if you're not careful and, for instance,forget to specify -ccc-gcc-namearmv7l-linux-gnueabihf-gcc
(whichuses hard-float), Clang will pick the armv7l-linux-gnueabi-ld
(which uses soft-float) and linker errors will happen.
Run Mac App On Linux
The same is true if you're compiling for different ABIs, like gnueabi
and androideabi
, and might even link and run, but produce run-timeerrors, which are much harder to track down and fix.