Is lib{library name}.a / .so a naming convention for static libraries in Linux?
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开发者_如何学JAVA 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?
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
.
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.
Short answer, yes that is the convention.
g++'s -l option will check for lib{somename}.so in your lib and local paths.
However in UNIX, you can also make use of symbolic links, so you can have different versions of libraries, without having to modify your make scripts.
edit to add:
As someone pointed out in the comment, .a
is the extension for a static library, while .so
is a Shared library.
Actually, no. I mean, almost! You are mixing static libraries with shared libraries. Static libraries are .a
files, and shared libraries end with .so
.
To summarize, you're talking about shared libraries, ok? When linking your application with shared libs, you need to use the standard convention which is -lNAME
, where NAME belongs to libNAME.so
精彩评论