How to cross compile from Mac OS X to Linux x86?
Asked Answered
L

5

22

I'm running Mac OS X 10.5.8 and want to compile for target CentOS 5.3 with GCC 4.1.2. How could I:

  1. Compile GCC 4.1.2 toolchain and related tools?
  2. Use that tool to cross compile for target CentOS 5.3?

Any help is greatly appreciated!

Leong answered 21/11, 2009 at 11:44 Comment(1)
What solution did you end up going with?Echelon
L
15

Your simplest solution is to just run CentOS 5.3 in a VM (e.g. Sun VirtualBox). This requires minimal setup, has quite reasonable overhead (assuming an Intel Mac), and you'll be able to actually test and debug what you are building.

If you really insist on cross-compiling, you must build a cross-compiler. Instructions are here and here, but beware: it will likely take you several days to get it right, and then you'll still need a VM to test the result, so I don't see any point in doing it that way.

Launalaunce answered 22/11, 2009 at 6:44 Comment(2)
thanks for your reply. yes, your suggestion sounds good :D Спасибо!Leong
There's also crosstool-ng, however my experience with that on OS X was a bit painful in that it required building within a case-sensitive disk image (it threw a fit if it were done on a case-preserving FS as is the HFS+ default), and one has to install a number of GNU or updated versions of certain system utilities in order for it to attempt a build on OS X.Houchens
W
8

Nowadays you can probably do it with Docker for Mac, I didn't test it because I have no mac. Docker basically creates a Linux VM and provides some nice-to-have functions.

Install docker and prepare your build image.

  • install docker
  • start a new container with docker run -ti centos5.3 /bin/bash (search the official Docker Hub for your desired target)
  • install your desired gcc version (something like sudo yum group install "Development Tools")
  • exit your container
  • run docker ps -a to obtain your container id
  • backup your container as base build image docker commit [id] centos:build

Make a build

Now you can use your created build environment for CentOS builds.

  • to start your build environment while mounting the working directory inside it you can use something like docker run -it --mount type=bind,source=$(pwd),target=/mnt centos:build /bin/sh -c "cd /mnt && bash"
  • then run gcc ... or ./configure or make or ninja or whatever to do your build
  • you can also run automated tests here if you wrote some

Docker vs VM

With docker you can use your beloved terminal with your familiar theme and keymap. Furthermore it most probably will consume less resources for startup and while running.

If your app is graphical and you test it by using interaction with its GUI I guess a VM is still a better option (see @employed-russian's solution).

Wast answered 7/1, 2019 at 23:23 Comment(0)
B
4

Use Cocotron to cross-compile directly from Xcode. There's a good getting-started guide here: http://blog.tlensing.org/tag/cocotron/

Bishop answered 28/9, 2012 at 13:28 Comment(0)
B
4

I struggled to get crosstool-ng to build on OSX 10.9 for x86_64 Linux. I used the very helpful post on how to build a gcc cross-compiler by Jeff Preshing and his script here as the basis for a script that worked for me. His script in the link doesn't work when compiling glibc on my version of OSX (which seems to be a very common complaint for those cross-compiling from OSX to Linux).

My script is here: https://github.com/johnlondon/Cross-compile-toolchain-for-linux-on-OSX

Bronwyn answered 26/8, 2016 at 18:16 Comment(0)
A
2

You can use crosstool-ng, or if you don't want to spend too much time to configure it, you can download a pre-configured cross-compiler, as Linaro. Here's the guide for installing it in Mac OS X.

Alkoran answered 25/3, 2016 at 20:16 Comment(1)
The Linaro toolchains are for ARM. The OP was looking for x86.Dominions

© 2022 - 2024 — McMap. All rights reserved.