首页 > 解决方案 > nasm - 无法在 macOS Mojave 上将目标文件与 ld 链接

问题描述

我正在尝试组装一个简单的 Hello World,它在以前的 macOS 版本中运行良好:

        global   start
        section  .text
start:  mov      rax, 0x02000004
        mov      rdi, 1
        mov      rsi, msg
        mov      rdx, 13
        syscall
        mov      rax, 0x02000001
        xor      rdi, rdi
        syscall

        section  .data
msg:    db       "Hello world!", 10

然后我像以前一样使用nasmand :ld

$ nasm -f macho64 hello.asm
$ ld hello.o -o hello

但是ld给了我以下错误:

ld: warning: No version-min specified on command line
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for inferred architecture x86_64

我尝试切换start_main,但得到以下结果:

ld: warning: No version-min specified on command line
ld: dynamic main executables must link with libSystem.dylib for inferred architecture x86_64

甚至不知道这可能意味着什么。

标签: macosassemblynasmld

解决方案


ld需要-lSystem标志来防止它抛出这个错误。它还需要-macosx_version_min删除警告。正确的使用ld方法是:ld hello.o -o hello -macosx_version_min 10.13 -lSystem.

在 macOS 11 及更高版本上更新,您也需要通过-L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib,以便它-lSystem正确定位库。如果需要,您可以使用-L$(xcode-select -p)/SDKs/MacOSX.sdk/usr/lib动态评估正确的路径。


推荐阅读