On Linux, the linker option --as-needed
is used to avoid keeping a reference to an unused library.
With this option, if -lxyz
is used in the link command but no symbol from libxyz.so
is used in the application, the executable will not load libxyz.so
. Conversely, with the linker option --no-as-needed
, the library will be loaded even if none of its symbols is referenced in the application. The default depends on the distro (see my previous question here).
What would be the equivalent option on macOS?
On macOS, the options --as-needed
and --no-as-needed
do not exist. The default behaviour is equivalent to --no-as-needed
, the libraries which were mentioned in the link command line are all loaded, even without symbol reference from the application. The command otool -L
on the executable always lists all libraries.
I cannot find any option to avoid loading unused libraries, no equivalent to --as-needed
.
Answering my own question after a few days of research:
-dead_strip_dylibs
-Wl,-dead_strip_dylibs