macos - 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
然后我像以前一样使用nasm
and :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
甚至不知道这可能意味着什么。
解决方案
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
动态评估正确的路径。
推荐阅读
- azure - Azure 可用性集
- javascript - .find() 多重选择器无法添加道具
- javascript - 无法使用 setState 或 forceUpdate 重新渲染/刷新组件
- jpa - Spring Data 如何使用 org.springframework.data.domain.ExampleMatcher 实现 not/not in/negate
- javascript - JS 地理位置无法正常运行
- node.js - Shelljs exec 在 git cherry-pick 上失败
- javascript - 单击时放大 SVG 并将单击的元素居中在视口中(d3.js)
- laravel - 在 laravel 中扩展的模型中定义主键的别名
- scala - java.lang.RuntimeException:com.datastax.bdp.fs.model.NoSuchFileException:找不到文件:/tmp/hive/
- django - 上下文处理器不适用于 Django 中的 Jinja2