Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Static library

From Wikipedia, the free encyclopedia
Software library used via static linking
icon
This articleneeds additional citations forverification. Please helpimprove this article byadding citations to reliable sources. Unsourced material may be challenged and removed.
Find sources: "Static library" – news ·newspapers ·books ·scholar ·JSTOR
(October 2013) (Learn how and when to remove this message)

Astatic library orstatically linked library containsfunctions and data that can be included in a consumingcomputer program atbuild-time such that the library does not need to be accessible in a separate file at run-time.[1] If all libraries are statically linked, then the resulting executable will bestand-alone, a.k.a. astatic build.

A static library is either merged with other static libraries andobject files at build-time to form a singleexecutable or loaded atrun-time into theaddress space of their corresponding executable at astatic memory offset determined at compile-time/link-time.

Comparison to dynamic linking

[edit]

Historically, all library linking was static, but todaydynamic 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 avoidsDLL Hell or more generallydependency 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 onWindows is to install a program's dynamic libraries with the program file.[2] OnUnix-like systems this is less common aspackage management systems can be used to ensure the correct library files are available in a shared location. Library files can be shared between applications. This can save space. The library can be updated to fix bugs and security flaws without updating each application that uses 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

[edit]

Any static library function can call a function or procedure in another static library. Thelinker and loader handle this the same way as for kinds of otherobject files. Static library files may be linked atrun time by alinking loader (e.g., theX11 module loader). However, whether such a process can be calledstatic linking is controversial.

Creating static libraries in C/C++

[edit]

Static libraries can be easily created inC or inC++. These languages providestorage-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 forexternal linkage (i.e. by not using the Cstatic keyword). Static library filenames usually have ".a" extension onUnix-like systems[1] and ".lib" extension onMicrosoft Windows.

For example, on a Unix-like system, to create an archive namedlibclass.a from filesclass1.o,class2.o,class3.o, the following command would be used:[1]

ar rcs libclass.a class1.o class2.o class3.o

to compile a program that depends onclass1.o,class2.o, andclass3.o, one could do:

cc main.c libclass.a

or (iflibclass.a is placed in the standard library path, like/usr/local/lib)

cc main.c -lclass

or (during linking)

ld ... main.o -lclass ...

instead of:

cc main.c class1.o class2.o class3.o

See also

[edit]

References

[edit]
  1. ^abc"Static Libraries". TLDP. Retrieved3 October 2013.
  2. ^Anderson, Rick (2000-01-11)."The End of DLL Hell". microsoft.com. Archived fromthe original on 2001-06-05. Retrieved2013-08-31.Private DLLs are DLLs that are installed with a specific application and used only by that application.
Parts,
conventions
Related topics
Retrieved from "https://en.wikipedia.org/w/index.php?title=Static_library&oldid=1331801419"
Category:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp