警告
如果您将其用于生产用例
该项目允许您在MACOS上执行的Linux上交叉编译代码。这对于要为MACOS构建的CI环境可能非常有用,但是您不想经历设置MacOS环境的麻烦(和成本)。
它支持:
对目标C和目标C ++的支持可能会在开箱即用的外部工作,但这未进行测试。
笔记
该项目的重点是支持MacOS和C,C ++,Fortran和Rust的新版本。比Macos 13(文图拉)年龄大的版本测试不佳,尽管它们应该正常。
MacOS系统库和标头配有Docker图像。这应该适合编译独立的MACOS程序和可能的本机MacOS应用程序。
跨编译器可作为Docker图像使用。这是分发项目的最简单方法,因为有很多主机依赖性。如果您有兴趣在没有Docker的情况下使用它,则应查看构成该项目基础的OSXCROSS。
Docker图像可在ghcr.io/shepherdjerred/macos-cross-compiler上找到。
安装下面的要求,然后按照“使用”部分中的说明进行操作。
重要的
Docker图像很大。它包括多个编译器和MacOS SDK。
# Start a Docker container using the Docker image.
# Replace `$PWD/samples` with the path to the source you want to compile.
$ docker run --platform=linux/amd64
-v $PWD /samples:/workspace
--rm
-it
ghcr.io/shepherdjerred/macos-cross-compiler:latest
/bin/bash
# Now that you're inside of the Docker container, you can run the compilers.
# Compile using gcc
# # targeting darwin arm64
$ aarch64-apple-darwin24-gcc hello.c -o hello
$ aarch64-apple-darwin24-g++ hello.cpp -o hello
# # targeting darwin x86_64
$ x86_64-apple-darwin24-gcc hello.c -o hello
$ x86_64-apple-darwin24-g++ hello.cpp -o hello
# Compile using clang
# # for darwin arm64
$ aarch64-apple-darwin24-clang --target=aarch64-apple-darwin24 hello.c -o hello
$ aarch64-apple-darwin24-clang++ --target=aarch64-apple-darwin24 hello.cpp -o hello
# # for darwin x86_64
$ x86_64-apple-darwin24-clang --target=x86_64-apple-darwin24 hello.c -o hello
$ x86_64-apple-darwin24-clang++ --target=x86_64-apple-darwin24 hello.cpp -o hello
# Compile using gfortran
# # for darwin arm64
$ aarch64-apple-darwin24-gfortran hello.f90 -o hello
# # for darwin x86_64
$ x86_64-apple-darwin24-gfortran hello.f90 -o hello
# Compile using Zig
# # C targeting darwin arm64 (change aarch64 -> x86_64 to target amd64)
$ zig cc
-target aarch64-macos
--sysroot=/sdk
-I/sdk/usr/include
-L/sdk/usr/lib
-F/sdk/System/Library/Frameworks
-framework CoreFoundation
-o hello hello.c
# # Rust targeting darwin arm64 (change aarch64 -> x86_64 to target amd64)
$ export CC=zig-cc-aarch64-macos
$ cd rust && cargo build --target aarch64-apple-darwin对RUST的支持需要一些项目配置。
# .cargo/config.toml
[ build ]
[ target . aarch64-apple-darwin ]
linker = " zig-cc-aarch64-macos "
[ target . x86_64-apple-darwin ]
linker = " zig-cc-x86_64-macos "配置后,您可以在设置CC变量之后运行cargo :
export CC= " zig-cc-x86_64-macos "
cargo build --target x86_64-apple-darwin
export CC= " zig-cc-aarch64-macos "
cargo build --target aarch64-apple-darwin下表显示了每个体系结构/编译器对可执行文件的名称。
笔记
目标内核版本是darwin24 。如果要支持其他内核版本,则需要构建新的Docker映像。
| aarch64 | |
|---|---|
| 铛 | AARCH64-APPLE-DARWIN24-CLANG |
| clang ++ | Aarch64-Apple-darwin24-clang ++ |
| 海湾合作委员会 | AARCH64-APPLE-DARWIN24-GCC |
| G ++ | AARCH64-APPLE-DARWIN24-G ++ |
| Gfortran | AARCH64-APPLE-DARWIN24-GFORTRAN |
相关编译器位于/osxcross/bin和/gcc/bin 。这两个目录已经在Docker容器中的PATH上。
该项目编译了Cctools,这是Apple版本的Binutils。这些程序是编译器,例如Archiver ar ,Loader ld和Assemberer as编译器使用的低级实用程序。
您可能不需要直接运行这些程序,但是如果这样做,它们位于/cctools/bin ,它们也在PATH上。
完整的工具列表:
该项目应该可以使用代码签名(但不是公证),但未经测试。建造通用二进制文件也应该是可能的,但是再次进行了测试。
建议将rcodesign作为MacOS签名和公证二进制的一种方式。
该项目可以在X86_64和AARCH64构图上为MacOS构建,而不论主机架构如何。
| Linux X86_64 | Linux ARM64 | |
|---|---|---|
| MacOS X86_64 | ✅ | ✅ |
| MacOS Aarch64 | ✅ | ✅ |
笔记
Aarch64是Apple的ARM64的内部名称。它们可以互换使用,但是在指代ARM64上的MacOS时,Aarch64更正确。
该项目支持以下语言:
该项目支持MacOS的以下版本:
对MacOS 15 Seqouia的支持尚未进行广泛测试。 Docker映像默认使用15.0 SDK。
重要的
该项目对MacOS,Clang和GCC的现代Verison进行了测试。尚未对这些软件的较旧版本进行测试。如果您需要与较旧版本的兼容,请查看OSXCROSS项目。
此Docker图像将Joseluisq/MacOSX-SDK的Xcode SDK捆绑在一起。请熟悉SDK的服务条款。
该存储库本质上是围绕以下项目的包装器:
这些资源在从事该项目时很有帮助:
这些资源告知了曲折的部分:
该存储库的Docker图像是用尘世的构建的。
提示
建造海湾合作委员会是计算和内存的强度。您可以通过将一个--cores=<number>参数传递给任何命令,例如earthly +image --cores=4来调整并行任务的数量。根据我的经验,如果您想使用所有内核,则需要大量的内存。我将这个价值设置为大约一半的核心,我取得了成功。如果您的机器失速,则应降低该值。
# Create a Docker image tagged as `shepherdjerred/macos-cross-compiler`
# The first run will take ~20 minutes on an M1 MacBook.
# Subsequent runs are faster.
earthly +image
# Verify that the compilers work correctly
earthly +test
# If you're on macOS, try actually running the binaries
earthly +validate没有OSXCROSS项目,该项目将是不可能的。