Cross Compiling For Mac On Linux



  • @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.
  • Using these two, we can compile a program in a Mac that will run on that Linux like this: crystal build yourprogram.cr -cross-compile -target 'x8664-unknown-linux-gnu' This will generate a.o ( Object file ) and will print a line with a command to execute on the system we are trying to cross-compile to.

Is there a way to cross compile for Mac OS X and iOS on Ubuntu? I found mingw-w64 packages and they work well for Windows and it seems I can cross compile for Android as well with gcc-arm-linux packages but I couldn't find an equivalent for Darwin X. I understand there might be some legal issues when dealing with Apple, but Fedora has cross compiler for Darwin X so I'm not sure. My first thought was to use my Linux machine at home. I can install the Rust compiler there and compile the server on that machine. Alternatively, I could use a virtual machine running Linux. Both of these options seemed slightly annoying. I've been curious how much work it would take to cross-compile, and I found this great post from Tim Ryan.

  • 2020-09-28 08:56:57
  • OfStack

Golang supports cross-compilation, which generates executables on one platform for another. Recently, it has been used 1 times, which is very useful. Note 1 here.

Compile Linux and Windows 64-bit executables under Mac

Compile Mac and Windows 64-bit executables under Linux

Mac and Linux 64-bit executables are compiled under Windows

GOOS: Operating system for target platform (darwin, freebsd, linux, windows)GOARCH: Architecture of the target platform (386, amd64, arm)

Cross-compilation does not support CGO so disable it

The above command compiles 64-bit executables, so you should certainly compile 32-bit executables using 386

You may see the following compile command in many tutorials on the web

CGO_ENABLED=0 means that the GO compiler is used with the C version. When the parameter is configured to 0, the COMPILER of C version is turned off. go has been compiled using the go language compiler since golang1.5. It was found that the CGO_ENABLED parameter was not used in golang1.9 and still compiled normally. Of course, the use of normal compilation can also be. For example, I set the CGO_ENABLED parameter to 1, that is, I used the CGO compiler during the compilation process, and I found that it can still compile normally.

In fact, if C libraries are used in go, for example, import 'C' will start the CGO compiler when go build is used by default. Of course, we can use CGO_ENABLED=0 to control whether go build USES the CGO compiler or not.

Many blogs have mentioned adding support for other platforms first, but I skipped that step, all of the commands listed above succeeded and got the results I wanted, so the step should be optional or the version of Go I'm using already supports all platforms by default.

In my previous posts, I came to the realization that the Raspberry Pi is not very fast! This results lots of chair spinning time while waiting for my projects to compile. A

After I did some brief research, I came across crosstool-ng. It enagbles anyone to create a toolchain to compile Raspberry Pi code directly on a (much faster) Macbook.

If you are unfamiliar with the process of compiling a toolchain on your own computer, let me be frank: it’s not fun.

Compiling

Install Linux On Mac

Luckily, with this post you can get yours working in no time.

Update 8/29/16: I have updated this tutorial in several places for newer systems. I’m currently running a Macbook Pro with 10.11.6.

Note 8/29: This tutorial now is focused on the Raspberry Pi 3 B. Steps can be tweaked to account for older Pis

So without further ado lets do this thing.

Before we get started

Before we start anything I recently compiled the toolchain for RPi3. It will save you a whole bunch of time to download it here rather than go through this procedure.

These files include:

  • Linux Kernel: 4.3

  • hardfp: yes

  • arch: armv8-a

  • proc: cortex-a53

  • glibc 2_22

  • gcc 5.2.0

  • binutils 2.25.1

  • gdb 7.10

  • gmp 6.0.0

  • mpfr 3.1.3

  • mpc 1.0.3

For those who want to continue for giggles, by all means…

Install Homebrew

You will need Hombrew to install some dependencies. If you haven’t already installed it you can run the command below:

Install crosstool-ng

Note: a few other dependencies get installed when crosstool-ng is compiled. Be prepared to wait a little while everything assembles.

Install gettext

Note: this is more of a precaution then a requirement. I believe by the end of this process that this was not necessary for getting crosstool-ng to work.

Create two case-sensitive disk images

Open up Disk utility. Click on the New Image button.

![Create a new disk image using Disk Utility on OSX.](images/diskutility-newimage.jpg)

1. You need a disk at least 15GB in size. This will house all the source code and object files when all said and done.

![Create case sensitive OSX disk image.](images/create-disk-image.jpg)

2. The next disk can be exactly the same but only 250MB in size. (When fully compiled and compressed everything turned out to be around 107MB)

Note 8/29: for some reason there is a bug on OSX which prevents you from formating a case sensitive drive in Disk Utility. So create a non-case sensitive image and format it to a case sensitive one. The file system needs to be case sensitive.

Note* 8/29: Also, should you make a disk that is not the correct size, you can invoke the resize command to fix it!

Install GNU grep

Crosstools relies on the use of GNU grep. The grep built with OSX is not 100% exactly the same. So, let’s build it!

Note 8/29: this now can be done while installing crosstool-ng using the --with-grep option.

Edit paths.sh file

My paths.sh file was located here:

I changed the grep line from:

To:

Note 8/29: likely can be avoided with the note above. You can also edit your .bash_profile to temporarily set which grep to use

Load the Linux/GNU option

This will load a general Linux/GNU config. We’ll end up replacing the config but it gives us a good starting point.

Note 8/29: this is a better starting point than my original suggestion. The config file below will change the remaining settings to accomodate for the different processor.

Install config file

Download the config file here.

You will have to copy it to your case sensitive disk image and rename it to .config.

Modify the config file

Run the following in your working directory.

Change the following as needed. Note: This only needs to be changed if you change the names of the .dmg images.

Paths and misc options

Cross compiling for mac on linux operating system

Note: all of these are under the ** Paths ** section.

  • Local tarballs directory

    I used /Volumes/xtools-build-env/src. Make sure you set yours to your setup.

  • Working directory

    I used /Volumes/xtools-build-env/.build. Make sure you set yours to your setup.

  • Prefix directory

    I used /Volumes/xtools/${CT_TARGET}. Make sure you set yours to your setup.

Note: the next few settings are under the ** Extracting ** section.

  • Stop after extracting tarballs

    This option should be checked.

  • Parallel jobs

    Note 8/29: new version already has this value set. You can leave it be.

Download and extract the packages

Run the following command:

The build command will stop after extracting all the sources.

Change source file

In ./.build/src/binutils-2.25.1/gold/gold-threads.cc you will need to change the file at line 284. Here is the before and after code blocks:

Change it to:

Update the ulimit

Ulimit controls the amount of resources allowed by a shell instance. In this case we need to increase this limit in order to prevent compilation errors.

Undo some the extract only config option

Undo one of the config settings we changed earlier. Open up:

Paths and misc options.

Note: the next few settings are under the ** Extracting ** section.

  • Stop after extracting tarballs

    This option should be unchecked.

Begin the build!

Compiling

Run:

Cross Compiling For Mac On Linux Operating System

Play the waiting game

Depending on how fast your setup is it may take a few hours to compile fully. If you’re impatient you can always get the binaries I just compiled here

In the end

By the time it’s done doing its thing you should have a fully capable cross platform toolchain for the Raspberry Pi! (Woot) An easy way to test it is to do the following:

(Hit ctrl-d to escape)

Copy test over to your Raspberry Pi.

Then ssh in and run the test executable

Other Notes

New notes as of 8/29 are as follows:

STOP/RESTART Crosstools now has a nifty stop and restart feature. Should a build break on a particular sub-component, you can actually fix the issue and continue the build from where it broke. It saves a ton of time. In order to take advantage of the feature you need to enable CT_DEBUG_CT_SAVE_STEPS in your .config

Then you can invoke the STOP or RESTART command:

ct-ng list-stepsct-ng build RESTART=cc_core_pass_1

Building Static Becuase OSX does not build based on static libraries we need to make sure those options are disabled. This is already done in my config file but for those who are interested here are the flags:

Thank you to Rolando for posting this in the comments!

Many thanks

Cross Compile For Mac On Linux

I used several blog posts and articles over the web to get this to work. Many thanks to their previous efforts.

Last Modified: 2020.3.7