首页 > 解决方案 > How to link mach-o format object files on linux?

问题描述

I have been attempting to link a MACHO formatted object file on Linux, but I have failed miserably. So far, I have created the object file by running:

nasm -fmacho -o machoh.o hello.o

I have tried linking using:

 clang --target=x86_64-apple-darwin machoh.o

but that failed. I have attempted using GCC, LD, and other linkers but I have still failed miserably. Are there any ideas on how I could solve my problem?

Thank you very much.

标签: linuxlinkerldmach-o

解决方案


最容易获得的解决方案是lldLLVM 链接器。

  1. lld 不附带 clang,而是一个单独的包。

    sudo apt install lld
    

    如果您安装了非默认的 clang 版本(例如clang-12显式),那么您应该为 lld 使用相同的版本(即lld-12)。

  2. 从某个地方获取 MacOS SDK。这个 GitHub 存储库将它们存档。

    如果您对使用上述内容感到不舒服,那么在没有 Mac 的情况下获得它的“合法”方式是:

    1. 创建 Apple ID
    2. 转到https://developer.apple.com/download/all/
    3. 下载“Xcode <版本>的命令行工具”
    4. 挂载或提取 dmg
    5. 提取 XAR 包
    6. 对于里面的每个“.pkg”文件夹,运行pbzx <Payload | cpio -i
    7. 找到Library/Developer/CommandLineTools/SDKs/MacOSX.sdk里面。
  3. 将上述两者都喂给叮当声:

    clang --target=x86_64-apple-darwin -fuse-ld=lld --sysroot=path/to/MacOSX.sdk machoh.o
    

推荐阅读