nasm - 无法在 macOS Mojave 上使用 ld link 目标文件
nasm - Can't link object file with ld on macOS Mojave
我正在尝试 assemble 一个简单的 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
和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
.
Updated 在 macOS 11 及更高版本上,您还需要传递 -L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib
以便它正确定位 -lSystem
库。如果需要,您可以使用 -L$(xcode-select -p)/SDKs/MacOSX.sdk/usr/lib
动态评估正确的路径。
除了上面的@Verloren 回答 ()
我遇到了 macOS Big Sur (macOS 11.1) 的问题,其中标记 -lSystem
无法找到 libSystem.dylib
,错误为
ld: library not found for -lSystem
我找到了 macOS Big Sur,引用自 link:
https://developer.apple.com/documentation/macos-release-notes/macos-big-sur-11_0_1-release-notes
New in macOS Big Sur 11.0.1, the system ships with a built-in dynamic
linker cache of all system-provided libraries. As part of this change,
copies of dynamic libraries are no longer present on the filesystem.
Code that attempts to check for dynamic library presence by looking
for a file at a path or enumerating a directory will fail...
动态库的所有副本都不位于 usr/lib/
和类似的位置,因此默认情况下无法找到标志 -lSystem
libSystem.dylib
。
这个问题的解决方案是 update/install 最新版本的命令行工具(如果还没有),并将 ld
命令的标志 -L
设置为 /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib
.
完整命令如下所示:
ld hello.o -o hello -macosx_version_min 11.0 -L /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib -lSystem
更简单的答案。 ld 默认为动态链接并尝试加载正在寻找 main 的 crt1。所以指定静态链接。
% ld -e start -static hello.o -o hello
% ./hello
Hello world!
在 macOS 11.2 中我使用了:
ld hello.o -L /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib -lSystem
我正在尝试 assemble 一个简单的 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
和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
.
Updated 在 macOS 11 及更高版本上,您还需要传递 -L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib
以便它正确定位 -lSystem
库。如果需要,您可以使用 -L$(xcode-select -p)/SDKs/MacOSX.sdk/usr/lib
动态评估正确的路径。
除了上面的@Verloren 回答 (
我遇到了 macOS Big Sur (macOS 11.1) 的问题,其中标记 -lSystem
无法找到 libSystem.dylib
,错误为
ld: library not found for -lSystem
我找到了 macOS Big Sur,引用自 link: https://developer.apple.com/documentation/macos-release-notes/macos-big-sur-11_0_1-release-notes
New in macOS Big Sur 11.0.1, the system ships with a built-in dynamic linker cache of all system-provided libraries. As part of this change, copies of dynamic libraries are no longer present on the filesystem. Code that attempts to check for dynamic library presence by looking for a file at a path or enumerating a directory will fail...
动态库的所有副本都不位于 usr/lib/
和类似的位置,因此默认情况下无法找到标志 -lSystem
libSystem.dylib
。
这个问题的解决方案是 update/install 最新版本的命令行工具(如果还没有),并将 ld
命令的标志 -L
设置为 /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib
.
完整命令如下所示:
ld hello.o -o hello -macosx_version_min 11.0 -L /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib -lSystem
更简单的答案。 ld 默认为动态链接并尝试加载正在寻找 main 的 crt1。所以指定静态链接。
% ld -e start -static hello.o -o hello
% ./hello
Hello world!
在 macOS 11.2 中我使用了:
ld hello.o -L /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib -lSystem