Introduction
On Windows, a .lib file is a library for the linker, made by the tools in
Microsoft Visual Studio and by
compatible ones such as LLVM. It comes in two kinds that share one format. A static
library holds COFF object files, and the linker
copies the ones a program needs into it. An import library holds no code: it tells the
linker which functions a DLL exports, so that a program can call them and Windows can
load the DLL when the program starts.
The format is a variant of the Unix ar archive.
Libraries from 16-bit DOS and Windows compilers also use .lib, but hold
OMF object files in a different library format
that starts with the byte 0xF0.
Structure
The file starts with !<arch>\n, followed by members, each with a
60-byte text header as in a Unix archive. The first two members are both named
/: the first linker member, an index of public symbols in big-endian
order kept for compatibility, and the second linker member, a sorted little-endian
index that Microsoft’s linker uses. A member named // holds long member
names. The remaining members are the object files.
In an import library, each member is usually a short import record instead of a full
object file. It starts with the 16-bit values 0 and 0xFFFF, then gives the
machine type, the name or ordinal of the imported function, and the DLL’s name.
0signature!<arch>\n8 bytes8first linker member/: symbol offsets, big-endian60 bytes + size…second linker member/: sorted symbol index, little-endian60 bytes + size…longnames member//, if any60 bytes + size…members: COFF object files or short import recordsvaries
Links
Software
- Visual Studio, with
libto make them anddumpbinto list them (Windows) - LLVM, with
llvm-lib,llvm-arandllvm-nm(Windows, macOS, Linux) - 7-Zip, which can list and extract the members (Windows)
Developers
- object, Rust crate that reads archives, COFF objects and import records
Reference
- PE Format, Microsoft’s specification, with sections on the archive (library) and import library formats