.lib
   HOME

TheInfoList



OR:

A static library or statically linked library contains functions and data that can be included in a consuming
computer program A computer program is a sequence or set of instructions in a programming language for a computer to Execution (computing), execute. It is one component of software, which also includes software documentation, documentation and other intangibl ...
at build-time such that the library does not need to be accessible in a separate file at run-time. If all libraries are statically linked, then the resulting executable will be
stand-alone Standalone or Stand-alone may refer to: *Stand-alone DSL, a digital subscriber line without analog telephone service; also known as ''naked DSL'' *Stand-alone expansion pack, an expansion pack which does not require the original game in order to us ...
, a.k.a. a
static build A static build is a compiled version of a program which has been statically linked against libraries. Linking In computer science, linking means taking one or more objects generated by compilers and assembling them into a single executable pro ...
. A static library is either merged with other static libraries and
object file An object file is a file that contains machine code or bytecode, as well as other data and metadata, generated by a compiler or assembler from source code during the compilation or assembly process. The machine code that is generated is kno ...
s at build-time to form a single
executable In computer science, executable code, an executable file, or an executable program, sometimes simply referred to as an executable or binary, causes a computer "to perform indicated tasks according to encoded instruction (computer science), in ...
or loaded at run-time into the
address space In computing, an address space defines a range of discrete addresses, each of which may correspond to a network host, peripheral device, disk sector, a memory cell or other logical or physical entity. For software programs to save and retrieve ...
of their corresponding executable at a static memory offset determined at compile-time/link-time.


Comparison to dynamic linking

Historically, all library linking was static, but today
dynamic linking In computing, a dynamic linker is the part of an operating system that loads and links the shared libraries needed by an executable when it is executed (at " run time"), by copying the content of libraries from persistent storage to RAM, fill ...
is an alternative and entails inherent trade-offs. An advantage of static over dynamic is that the application is guaranteed to have the library routines it requires available at run-time, as the code to those routines is embedded in the executable file. With dynamic linking, not only might the library file be missing, but even if found, it could be an incompatible version. Static avoids DLL Hell or more generally
dependency hell Dependency hell is a colloquial term for the frustration of some software users who have installed software packages which have dependencies on specific versions of other software packages. The dependency issue arises when several packages ha ...
and therefore can simplify development, distribution and installation. Another trade-off is memory used to load the library. With static linking, a smart linker only includes the code that is actually used, but for a dynamic library, the entire library is loaded into memory. Another trade-off is that the size of the executable is larger with static linking than dynamic. But, if the size of an application is measured as the sum of the executable and its dynamic libraries, then overall size is generally less for static. Then again, if the same dynamic library is used by multiple applications, then overall size of the combined applications plus DLLs might be less with dynamic. A common practice on
Windows Windows is a Product lining, product line of Proprietary software, proprietary graphical user interface, graphical operating systems developed and marketed by Microsoft. It is grouped into families and subfamilies that cater to particular sec ...
is to install a program's dynamic libraries with the program file. On
Unix-like A Unix-like (sometimes referred to as UN*X, *nix or *NIX) operating system is one that behaves in a manner similar to a Unix system, although not necessarily conforming to or being certified to any version of the Single UNIX Specification. A Uni ...
systems this is less common as
package management system A package manager or package management system is a collection of software tools that automates the process of installing, upgrading, configuring, and removing computer programs for a computer in a consistent manner. A package manager deals wi ...
s can be used to ensure the correct library files are available in a shared, system location. This allows library files to be shared between applications leading to space savings. It also allows the library to be updated to fix bugs and security flaws without updating the applications that use the library. But shared, dynamic libraries leads to the risk of dependency problems. In practice, many executables use both static and dynamic libraries.


Linking and loading

Any static library function can call a function or procedure in another static library. The
linker Linker or linkers may refer to: Computing * Linker (computing), a computer program that takes one or more object files generated by a compiler or generated by an assembler and links them with libraries, generating an executable program or shar ...
and loader handle this the same way as for kinds of other
object file An object file is a file that contains machine code or bytecode, as well as other data and metadata, generated by a compiler or assembler from source code during the compilation or assembly process. The machine code that is generated is kno ...
s. Static library files may be linked at run time by a
linking loader A linker or link editor is a computer program that combines intermediate software build files such as object and library files into a single executable file such as a program or library. A linker is often part of a toolchain that includes a comp ...
(e.g., the
X11 The X Window System (X11, or simply X) is a windowing system for bitmap displays, common on Unix-like operating systems. X originated as part of Project Athena at Massachusetts Institute of Technology (MIT) in 1984. The X protocol has been at ...
module loader). However, whether such a process can be called ''static linking'' is controversial.


Creating static libraries in C/C++

Static libraries can be easily created in C or in C++. These two languages provide storage-class specifiers for indicating external or internal linkage, in addition to providing other features. To create such a library, the exported functions/procedures and other objects variables must be specified for ''
external linkage In programming languages, particularly the compiled ones like C, C++, and D, linkage describes how names can or can not refer to the same entity throughout the whole program or one single translation unit. The static keyword is used in C to re ...
'' (i.e. by not using the C static keyword). Static library filenames usually have "" extension on
Unix-like A Unix-like (sometimes referred to as UN*X, *nix or *NIX) operating system is one that behaves in a manner similar to a Unix system, although not necessarily conforming to or being certified to any version of the Single UNIX Specification. A Uni ...
systems and "" extension on
Microsoft Windows Windows is a Product lining, product line of Proprietary software, proprietary graphical user interface, graphical operating systems developed and marketed by Microsoft. It is grouped into families and subfamilies that cater to particular sec ...
. For example, on a Unix-like system, to create an archive named from files , , , the following command would be used: ar rcs libclass.a class1.o class2.o class3.o to compile a program that depends on , , and , one could do: cc main.c libclass.a or (if is placed in standard library path, like ) cc main.c -lclass or (during linking) ld ... main.o -lclass ... instead of: cc main.c class1.o class2.o class3.o


See also

*
Static build A static build is a compiled version of a program which has been statically linked against libraries. Linking In computer science, linking means taking one or more objects generated by compilers and assembling them into a single executable pro ...
*
Library (computing) In computing, a library is a collection of System resource, resources that can be leveraged during software development to implement a computer program. Commonly, a library consists of executable code such as compiled function (computer scienc ...
*
Linker (computing) A linker or link editor is a computer program that combines intermediate software build files such as object file, object and library (computing), library files into a single executable file such as a program or library. A linker is often part o ...
*
Loader (computing) In computing, computer systems a loader is the part of an operating system that is responsible for loading computer program, programs and Library (computing), libraries. It is one of the essential stages in the process of starting a program, as i ...
*
Shared library In computing, a library is a collection of System resource, resources that can be leveraged during software development to implement a computer program. Commonly, a library consists of executable code such as compiled function (computer scienc ...
*
Dynamic-link library A dynamic-link library (DLL) is a shared library in the Microsoft Windows or OS/2 operating system. A DLL can contain executable code (functions), data, and resources. A DLL file often has file extension .dll even though this is not required ...
(DLL, .dll) *
External variable In the C programming language, and its predecessor B, an external variable is a variable defined outside any function block. On the other hand, a local (automatic) variable is a variable defined inside a function block. Definition, declarati ...
*
Object file An object file is a file that contains machine code or bytecode, as well as other data and metadata, generated by a compiler or assembler from source code during the compilation or assembly process. The machine code that is generated is kno ...
*
Prebinding In computing, prebinding, also called prelinking, is a method for optimizing application load times by resolving library symbols prior to launch. Background Most computer programs consist of code that requires external shared libraries to execu ...
*
JAR (file format) A JAR ("Java archive") file is a package file format typically used to aggregate many Java class files and associated metadata and resources (text, images, etc.) into one file for distribution. JAR files are archive files that include a Java-s ...


References

{{Application binary interface Computer libraries