Working with libraries
- For predictable results, the same set of options used to generate code (see "Code Gen Options" and "Submodel Options" in the gcc Info pages) must be used to link (see "Link Options" in the gcc Info pages)!
- -shared should be supplied with the link step to produce a shared object.
- Regarding -fpic vs -fPIC: -fPIC is only necessary in certain architecture-dependent situations; gcc will let you know if it's needed, and fail out, should you provide -fpic
- Use -Wl,ldopt to pass options to ld (see below)
- Generally, either the packaging system or build process needs invoke ldconfig with suitable arguments so that the linker cache is updated.
- The -t option (-Wl,-t as a gcc option) traces input files to ld, and can be useful for debugging, as can -y symbol to trace a given symbol's introduction and use.m
- soname=name sets the internal DT_SONAME field of an ELF object
- -fini name and -init name to specify alternative values for the ELF DT_INIT and DT_FINI initialization and finalization fields. By default, _init and _finit are used.
- -rpath=path (-R directory on some linkers; GNU ld supports this) sets the DT_RUNPATH (DT_RPATH without new dtags) dtag, controlling rpaths
- --enable-new-dtags is required to generate DT_RUNPATH or DT_FLAGS dtags specified by newer ELF systems.
Building with libraries
- If the library supports pkg-config, and you're willing to depend on that tool being installed, proper compilation flags can be lifted from it:
- pkg-config --cflags pkgname will pull the preprocessor and compilation flags
- pkg-config --libs pkgname will pull the linking flags
- pkg-config --list-all lists all libraries registered with pkg-config
Environment variables that control ld.so
Files that control ld.so
- Use ldd's command line options to check the library.
- Discover unused direct dependencies with -u -r:
[recombinator](1) $ ldd -u -r /usr/lib/libapr-1.so.0 Unused direct dependencies: /lib/librt.so.1 /lib/libcrypt.so.1 [recombinator](1) $ ldd -u -r .out/libdank/libdank.so Unused direct dependencies: [recombinator](1) $
- ld warn options, especially --warn-shared-textrel, --fatal-warnings, --warn-common, etc
- ELF / rpaths
- Interesting libraries / Glibc
- Library entry on LQWiki
- Ulrich Drepper's excellent paper, "How to Write Shared Libraries"
- Gold, a new linker introduced to binutils in March 2008. As of November 2008, it's not been a part of any binutils release.
- Solaris 9 dtag documentation from Sun
- This 1997-11 LinuxFocus article is old but pretty good
- The Linux Documentation Project's Shared Library HOWTO