I've had to do some minor programming on a Ubuntu system recently (at which I am an extremely low-level beginner) and I'm really just getting familiar with makefiles.
I noticed that the arguments to tell the linker which libraries to include were always -l{library name} where the corresponding library would be something called "lib{library name}.a" in the /usr/lib folder.
I am wondering: is that a convention? I would have thought I would need to type -llibNAME to find a library called libNAME.a, but it seems to assume a lib prefix.
Is this always the case? Can I name a library without using a lib prefix?
A . a file is a static library, while a . so file is a shared object (dynamic) library similar to a DLL on Windows.
a files are static libraries. The suffix stands for "archive", because they're actually just an archive (made with the ar command -- a predecessor of tar that's now just used for making libraries) of the original .o object files.
Static libraries are simply a collection of ordinary object files; conventionally, static libraries end with the ``. a'' suffix. This collection is created using the ar (archiver) program.
In programming, a library is a collection of pre-compiled pieces of code. A library can be reused in different programs. In Linux, libraries can be categorized into: Static libraries: bound to a program statically at compile time. Shared libraries: loaded when a program launches and loaded into memory at runtime.
name.a
is a static library (a
because it's an archive of objects).
name.so
is a dynamic library (so
because it's a shared object, also sometimes known as a DSO, for Dynamic Shared Object).
The -lfoo
linker switch traditionally assumes a name of the form libfoo.{so,a}
, and searches for it on the library path. You can also pass a library name to the linker directly (without using the -l
switch), but in that case you have to pass the path to the library explicitly.
As @geekosaur
noted, if you open a shared object at runtime, dlopen()
takes the full filename.
You can name one any way you want, but ld
's -l
assuming a lib
prefix applies to both static and shared libraries and goes back a long way; you'd need to name it explicitly to use one without the lib
prefix.
This is actually useful even on modern systems: a name libfoo.so
can be identified as a link-time library, while foo.so
indicates a shared object implementing a runtime plugin. Or subsystem-specific prefixes in place of lib
to identify plugins for particular subsystems; see for example pam_*.so
and nss_*.so
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With