# 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: tutorial

Language: en

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

Topics: [Objective-C](<https://devfeed.tech/topics/objective-c.md>), [Swift](<https://devfeed.tech/topics/swift.md>), [toolchain](<https://devfeed.tech/topics/toolchain.md>)

Tags: [assembly](<https://devfeed.tech/tags/assembly.md>), [clang](<https://devfeed.tech/tags/clang.md>), [objective-c](<https://devfeed.tech/tags/objective-c.md>), [swift](<https://devfeed.tech/tags/swift.md>)

## AI overview

This article explains automatic linking for Mach-O binaries. It shows how Objective-C compilation can embed linker options for dependencies such as Foundation, how to inspect those options, and how omitting module support causes explicit linking to be required. It also covers module maps, Swift standard-library dependencies, and several ways to add linker options manually with clang, swiftc, or assembly directives.

## 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