Static library

Software library used via static linking


title: "Static library" type: doc version: 1 created: 2026-02-28 author: "Wikipedia contributors" status: active scope: public tags: ["computer-libraries"] description: "Software library used via static linking" topic_path: "general/computer-libraries" source: "https://en.wikipedia.org/wiki/Static_library" license: "CC BY-SA 4.0" wikipedia_page_id: 0 wikipedia_revision_id: 0

::summary Software library used via static linking ::

A static library or statically linked library contains functions and data that can be included in a consuming computer program 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, a.k.a. a static build.

A static library is either merged with other static libraries and object files at build-time to form a single executable or loaded at run-time into the address space 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 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 and therefore can simplify development, distribution and installation.

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 the address space, which means that code that is not used by the program may be loaded into memory. However, if more than one program is using that code, only one copy of the shared code is loaded into memory.

The size of an executable is larger with static linking than dynamic, as the statically-linked executable includes copies of library routines used by the program. However, if the size of an application is measured as the sum of the executable and its dynamic libraries, then the overall size is generally less for static linking. But if the same dynamic library is used by multiple applications, then the overall size of the combined applications and the dynamic libraries might be less with dynamic linking.

A common practice on Windows is to install a program's dynamic libraries with the program file.{{cite web | url=http://msdn.microsoft.com/library/techart/dlldanger1.htm | title=The End of DLL Hell | date=2000-01-11 | archiveurl=https://web.archive.org/web/20010605023737/http://msdn.microsoft.com/library/techart/dlldanger1.htm | archivedate=2001-06-05 | last=Anderson | first=Rick | publisher=microsoft.com | accessdate=2013-08-31 | quote=Private DLLs are DLLs that are installed with a specific application and used only by that application.

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 and loader handle this the same way as for kinds of other object files. Static library files may be linked at run time by a linking loader (e.g., the X11 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 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 or procedures and other objects and variables must be specified for external linkage (i.e. by not using the C static keyword). Static library filenames usually have "" extension on Unix-like systems and "" extension on Microsoft Windows.

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 the 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

References

References

  1. "Static Libraries". TLDP.

::callout[type=info title="Wikipedia Source"] This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page. ::

computer-libraries