Friend: I tried looking at static linking in Mac OS X and it seems nearly impossible. Take a look at this http://stackoverflow.com/a/3801032
Me: I have no idea what that
-staticflag does, but I'm pretty sure that's not how you link to a library. Let me RTFM a bit.
Minutes later...
Me: I'm gonna have to write this stuff down.
First things first, gcc isn't the default compiler in Mac OS X anymore. Since Xcode 5, the Apple developer toolchain uses clang, and gcc only aliases to clang.
NOTE: All shell outputs in this document were produced with the default
bashon Mac OS X El Capitan 10.11.3 with Xcode 7.2 installed.
$ gcc -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.3.0
Thread model: posix
So let's look into clang.
$ man clang
clang is a C, C++, and Objective-C compiler which encompasses preprocessing,
parsing, optimization, code generation, assembly, and linking. Depending on
which high-level mode setting is passed, Clang will stop before doing a full
link. While Clang is highly integrated, it is important to understand the stages
of compilation, to understand how to invoke it. These stages are:
Driver
The clang executable is actually a small driver which controls the overall
execution of other tools such as the compiler, assembler and linker.
Typically you do not need to interact with the driver, but you transparently
use it to run the other tools.
Preprocessing
This stage handles tokenization of the input source file, macro expansion,
#include expansion and handling of other preprocessor directives. The output
of this stage is typically called a ".i" (for C), ".ii" (for C++), ".mi"
(for Objective-C) , or ".mii" (for Objective-C++) file.
Parsing and Semantic Analysis
This stage parses the input file, translating preprocessor tokens into a
parse tree. Once in the form of a parser tree, it applies semantic analysis
to compute types for expressions as well and determine whether the code is
well formed. This stage is responsible for generating most of the compiler
warnings as well as parse errors. The output of this stage is an "Abstract
Syntax Tree" (AST).
Code Generation and Optimization
This stage translates an AST into low-level intermediate code (known as
"LLVM IR") and ultimately to machine code. This phase is responsible for
optimizing the generated code and handling target-specific code generation.
The output of this stage is typically called a ".s" file or "assembly" file.
Clang also supports the use of an integrated assembler, in which the code
generator produces object files directly. This avoids the overhead of
generating the ".s" file and of calling the target assembler.
Assembler
This stage runs the target assembler to translate the output of the compiler
into a target object file. The output of this stage is typically called a
".o" file or "object" file.
Linker
This stage runs the target linker to merge multiple object files into an
executable or dynamic library. The output of this stage is typically called
an "a.out", ".dylib" or ".so" file.
So static linking is an option of the linker stage of compilation. So let's try ld's manual.
$ man ld
OPTIONS
Options that control the kind of output
-execute The default. Produce a mach-o main executable that has file
type MH_EXECUTE.
-dylib Produce a mach-o shared library that has file type MH_DYLIB.
-bundle Produce a mach-o bundle that has file type MH_BUNDLE.
-dynamic The default. Implied by -dylib, -bundle, or -execute
-static Produces a mach-o file that does not use the dyld. Only used
building the kernel.
Now we have it. -static does not control how the output links to libraries; it controls the type of output produced by the linker. In this case, -static is used to indicate that no dynamic linking should occur with this binary. Ever. The only file to ever need this option is the kernel.
So how does
$ man ld
Options that control libraries
-lx This option tells the linker to search for libx.dylib or libx.a in
the library search path. If string x is of the form y.o, then that
file is searched for in the same places, but without prepending
`lib' or appending `.a' or `.dylib' to the filename.
-Ldir Add dir to the list of directories in which to search for
libraries. Directories specified with -L are searched in the order
they appear on the command line and before the default search
path. In Xcode4 and later, there can be a space between the -L and
directory.
