Static library
This article needs additional citations for verification. (October 2013) |
In
Advantages and disadvantages
There are several advantages to statically linking libraries with an executable instead of
With static linking, it is enough to include those parts of the library that are directly and indirectly referenced by the target executable (or target library). With dynamic libraries, the entire library is loaded, as it is not known in advance which functions will be invoked by applications. Whether this advantage is significant in practice depends on the structure of the library.
In static linking, the size of the executable becomes greater than in dynamic linking, as the library code is stored within the executable rather than in separate files. But if library files are counted as part of the application then the total size will be similar, or even smaller if the compiler eliminates the unused symbols.
Environment specific
On
Linking and loading
Any static library function can call a function or procedure in another static library. The
Creating static libraries in C/C++
Static libraries can be easily created in
static
keyword). Static library filenames usually have ".a" extension on Unix-like systems[1] and ".lib" extension on Microsoft WindowsFor example, on a Unix-like system, to create an archive named libclass.a from files class1.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 on class1.o, class2.o, and class3.o, one could do:
cc main.c libclass.a
or (if libclass.a is placed in 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
- Static build
- Library (computing)
- Linker (computing)
- Loader (computing)
- Shared library
- Dynamic-link library (DLL, .dll)
- External variable
- Object file
- Prebinding
- JAR (file format)
References
- ^ a b c "Static Libraries". TLDP. Retrieved 3 October 2013.
- ^ Anderson, Rick (2000-01-11). "The End of DLL Hell". microsoft.com. Archived from the original on 2001-06-05. Retrieved 2013-08-31.
Private DLLs are DLLs that are installed with a specific application and used only by that application.