我一直在尝试在Linux上链接一个大男子格式化的目标文件,但我失败得很惨痛。到目前为止,我已经通过运行以下命令创建了对象文件:
nasm -fmacho -o machoh.o hello.o我尝试使用以下链接:
clang --target=x86_64-apple-darwin machoh.o但那失败了。我尝试过使用GCC、LD和其他链接器,但仍以失败告终。有什么办法可以解决我的问题吗?
非常感谢。
发布于 2021-10-14 15:18:14
最容易访问的解决方案是lld,它是LLVM链接器。
sudo apt install lld
如果您安装的clang版本不是默认版本(例如,显式安装的clang-12 ),那么您应该对lld使用相同的版本(即clang
如果你不喜欢使用上面的东西,那么在没有苹果电脑的情况下获得它的“合法”方式应该是:
1. Create an Apple ID
2. Go to [https://developer.apple.com/download/all/](https://developer.apple.com/download/all/)
3. Download the "Command Line Tools for Xcode <version>"
4. Mount or extract the dmg
5. Extract the XAR package
6. For each ".pkg" folder inside, run `pbzx <Payload | cpio -i`
7. Find the `Library/Developer/CommandLineTools/SDKs/MacOSX.sdk` inside.clang --target=x86_64-apple-darwin -fuse-ld=lld --sysroot=path/to/MacOSX.sdk machoh.o
发布于 2021-10-14 01:41:22
我尝试过使用:
clang --target=x86_64-apple-darwin machoh.o链接,但失败了。
失败原因是什么?细节很重要。
总之,Linux上有3个常用的链接器: BFD-ld、Gold和(最新的) LLD。
其中,Gold是一个仅限ELF的链接器,不适用于Mach-O。
在我的发行版中,BFD-ld仅被配置为支持几个仿真(使用ld --help查看哪些仿真)。BFD似乎确实支持Mach-O,因此使用这种支持构建Linux BFD-ld交叉链接器是可能的。
LLD应该支持开箱即用的Mach-O,但是您可能没有使用LLD。
因此,您的第一步应该是找出clang --target=x86_64-apple-darwin ...使用哪个链接器,然后让它使用支持Mach-O的链接器。
https://stackoverflow.com/questions/69557383
复制相似问题