nasm - Can't link object file with ld on macOS Mojave
ld
needs -lSystem
flag to prevent it from throwing this error. Also it needs -macosx_version_min
to remove the warning. The correct way of using ld
would be: ld hello.o -o hello -macosx_version_min 10.13 -lSystem
.
Updated on macOS 11 and above, you need to pass -L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib
as well so that it locates the -lSystem
library correctly. You can use -L$(xcode-select -p)/SDKs/MacOSX.sdk/usr/lib
to evaluate the right path dynamically if required.
In addition to the @Verloren answer above (https://stackoverflow.com/a/52830915/1189569)
I had an issue with macOS Big Sur (macOS 11.1), where flag -lSystem
could not locate libSystem.dylib
, with the error
ld: library not found for -lSystem
I found out for macOS Big Sur, quoted from the 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...
that all copies of dynamic libraries are not located in usr/lib/
and similar, so flag -lSystem
could not found libSystem.dylib
by default.
The solution to this was to update/install the latest version of Command Line Tools, if not already, and to set flag -L
of the ld
command to /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib
.
So full command would look like this:
ld hello.o -o hello -macosx_version_min 11.0 -L /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib -lSystem
Simpler answer. ld is defaulting to dynamic linking and tries to load crt1 which is looking for main. So specify static linking.
% ld -e start -static hello.o -o hello
% ./hello
Hello world!