Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 920
8bitcartridge Avatar asked Jul 03 '11 05:07

8bitcartridge


People also ask

Is .so a static library?

A . a file is a static library, while a . so file is a shared object (dynamic) library similar to a DLL on Windows.

What is a .a library file?

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.

What is static library in Linux?

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.

What are libs in Linux?

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.


2 Answers

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.

like image 25
ninjalj Avatar answered Nov 09 '22 19:11

ninjalj


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.

like image 100
geekosaur Avatar answered Nov 09 '22 21:11

geekosaur