macos 如何从 Mac OS X 交叉编译到 Linux x86?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1775267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 06:34:00  来源:igfitidea点击:

How to cross compile from Mac OS X to Linux x86?

gccmacoscentoscross-compilinggnu-toolchain

提问by Viet

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:

我正在运行 Mac OS X 10.5.8 并希望使用 GCC 4.1.2 编译目标 CentOS 5.3。我怎么能:

  1. Compile GCC 4.1.2 toolchain and related tools?
  2. Use that tool to cross compile for target CentOS 5.3?
  1. 编译 GCC 4.1.2 工具链和相关工具?
  2. 使用该工具为目标 CentOS 5.3 进行交叉编译?

Any help is greatly appreciated!

任何帮助是极大的赞赏!

采纳答案by Employed Russian

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 testand debugwhat you are building.

您最简单的解决方案是在 VM(例如 Sun VirtualBox)中运行 CentOS 5.3 。这需要最少的设置,具有相当合理的开销(假设是 Intel Mac),并且您将能够实际测试调试您正在构建的内容。

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

如果你真的坚持交叉编译,你必须构建一个交叉编译器。说明在这里这里,但请注意:您可能需要几天时间才能正确处理,然后您仍然需要一个 VM 来测试结果,所以我认为这样做没有任何意义。

回答by Nestor

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

使用 Cocotron 直接从 Xcode 交叉编译。这里有一个很好的入门指南:http: //blog.tlensing.org/tag/cocotron/

回答by adabru

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.

现在你可能可以用Docker for Mac来做,我没有测试它,因为我没有 mac。Docker 基本上创建了一个 Linux VM,并提供了一些不错的功能。

Install docker and prepare your build image.

安装 docker 并准备构建映像。

  • install docker
  • start a new container with docker run -ti centos5.3 /bin/bash(search the official Docker Hubfor your desired target)
  • install your desired gcc version (something like sudo yum group install "Development Tools")
  • exit your container
  • run docker ps -ato obtain your container id
  • backup your container as base build image docker commit [id] centos:build
  • 安装码头工人
  • 启动一个新容器docker run -ti centos5.3 /bin/bash(在官方Docker Hub 中搜索您想要的目标)
  • 安装您想要的 gcc 版本(类似sudo yum group install "Development Tools"
  • 退出你的容器
  • 运行docker ps -a以获取您的容器 ID
  • 备份你的容器作为基础构建镜像 docker commit [id] centos:build

Make a build

进行构建

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

现在您可以使用您创建的构建环境进行 CentOS 构建。

  • 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 ./configureor makeor ninjaor whatever to do your build
  • you can also run automated tests here if you wrote some
  • 在安装工作目录的同时启动你的构建环境,你可以使用类似的东西 docker run -it --mount type=bind,source=$(pwd),target=/mnt centos:build /bin/sh -c "cd /mnt && bash"
  • 然后运行gcc ...or ./configureor makeor ninjaor 什么来做你的构建
  • 如果你写了一些,你也可以在这里运行自动化测试

Docker vs VM

Docker 与 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.

使用 docker,您可以使用您钟爱的终端和您熟悉的主题和键盘映射。此外,它很可能会在启动和运行时消耗更少的资源。

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

如果您的应用程序是图形化的,并且您通过使用与其 GUI 的交互来测试它,我想 VM 仍然是更好的选择(请参阅 @paid-russian 的解决方案)。

回答by SegFault

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 guidefor installing it in Mac OS X.

你可以使用crosstool-ng,或者如果你不想花太多时间来配置它,你可以下载一个预先配置好的交叉编译器,如Linaro。这是在 Mac OS X 中安装它的指南

回答by John

I struggled to get crosstool-ngto build on OSX 10.9 for x86_64 Linux. I used the very helpful post on how to build a gcc cross-compilerby Jeff Preshing and his script hereas 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).

我努力让crosstool-ng在 OSX 10.9 上为 x86_64 Linux 构建。我用了很大的帮助后如何建立一个gcc交叉编译器杰夫Preshing和他的剧本在这里为对我来说有效的脚本的基础。在我的 OSX 版本上编译 glibc 时,他在链接中的脚本不起作用(对于从 OSX 到 Linux 的交叉编译,这似乎是一个非常普遍的抱怨)。

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

我的脚本在这里:https: //github.com/johnlondon/Cross-compile-toolchain-for-linux-on-OSX