# Auto linking with Mach-O binaries

DevFeed: [Auto linking with Mach-O binaries](<https://devfeed.tech/articles/auto-linking-with-mach-o-binaries-25407.md>)

Original publisher: [Read original article](<https://smileykeith.com/2022/02/23/lc-linker-option/>)

Author: Keith Smiley

Published: 2022-02-24T02:00:00Z

Content type: article

Language: en

Sources: [Keith Smiley](<https://devfeed.tech/sources/keith-smiley.md>)

Topics: [static linking](<https://devfeed.tech/topics/static-linking.md>)

Tags: [asm](<https://devfeed.tech/tags/asm.md>), [assembly](<https://devfeed.tech/tags/assembly.md>), [bazel](<https://devfeed.tech/tags/bazel.md>), [binaries](<https://devfeed.tech/tags/binaries.md>), [build-system](<https://devfeed.tech/tags/build-system.md>), [c-plus-plus](<https://devfeed.tech/tags/c-plus-plus.md>), [clang](<https://devfeed.tech/tags/clang.md>), [cmake](<https://devfeed.tech/tags/cmake.md>), [compile](<https://devfeed.tech/tags/compile.md>), [dependencies](<https://devfeed.tech/tags/dependencies.md>), [foundation](<https://devfeed.tech/tags/foundation.md>), [framework](<https://devfeed.tech/tags/framework.md>), [libraries](<https://devfeed.tech/tags/libraries.md>), [linker](<https://devfeed.tech/tags/linker.md>), [linking](<https://devfeed.tech/tags/linking.md>), [maps](<https://devfeed.tech/tags/maps.md>), [module](<https://devfeed.tech/tags/module.md>), [object](<https://devfeed.tech/tags/object.md>), [objective-c](<https://devfeed.tech/tags/objective-c.md>), [succeed](<https://devfeed.tech/tags/succeed.md>)

## AI overview

Mach-O auto linking embeds linker options in object files at compile time, reducing the need to repeat dependency flags at link time. In the Objective-C example, compiling with Clang's `-fmodules` records `Foundation` in an `LC_LINKER_OPTION`, so the later link succeeds without an explicit framework flag; without modules, linking fails until `-framework Foundation` is added. Module maps can also declare libraries and frameworks with `link` directives. Swift object files can record required Swift libraries, including implementation dependencies. When auto linking is unavailable, the excerpt shows Clang and Swift compiler flags for adding linker options, Clang's `--dependent-lib` for library dependencies, and assembly `.linker_option` directives. The compiler flag approach is described as unsuitable for framework options requiring multiple arguments; assembly directives handle spaces and are shown adding both a library and a framework.

## Source excerpt

Auto linking is a feature that embeds information in your binaries' at compile time which is then used at link time to automatically link your dependencies. This allows you to reduce the duplication of flags between the different phases of your (or your consumers') builds. For example, with this Objective-C file: #include <Foundation/Foundation.h> int main() { NSLog(@"Hello, World!"); return 1; } Compiled with: $ clang -fmodules -c foo.m -o foo.o You can then inspect the options added for use at link time: $ otool -l foo.o | grep LC_LINKER_OPTION -A3 cmd LC_LINKER_OPTION cmdsize 40 count 2 string #1 -framework string #2 Foundation ... Now when linking this binary you don't have to pass any extra flags to the linker to make sure you link Foundation: $ ld foo.o -syslibroot `xcrun --show-sdk-path` To compare, if you compile the binary without -fmodules1: $ clang -c foo.m -o foo.o You don't get any LC_LINKER_OPTIONs. Then when linking the binary with the same command as before, it fails with these errors: $ ld foo.o -syslibroot `xcrun --show-sdk-path` Undefined symbols for architecture arm64: "_NSLog", referenced from: _main in foo.o "___CFConstantStringClassReference", referenced from: CFString in foo.o ld: symbol(s) not found for architecture arm64 To make it succeed you must explicitly link Foundation through an argument to your linker invocation: $ ld foo.o -syslibroot `xcrun --show-sdk-path` -framework Foundation Auto linking is also applied when using module maps that use the link directive. For example with this module map file: // module.modulemap module foo { link "foo" link framework "Foundation" } That you include with in this source file: @import foo; int main() { return 1; } And compile (with an include path to the module.modulemap file): $ clang -fmodules -c foo.m -o foo.o -I. The produced object depends on foo and Foundation. This can be useful for handwriting module map files for prebuilt libraries, and for quite a few other cases. You can read about thi