|
||||
Libraries (Plagerised from http://www.dirac.org/linux/libraries.html)In general, there are two types of library files: dynamic libraries and static libraries.
I. Dynamic LibrariesDynamic libraries (also known as shared libraries) are pieces of compiled code which are loaded by the runtime linker /lib/ld.so when an executable is run. This is analagous to the .dll files of MS Windows. In terms of efficiency, this saves:
II. Static LibrariesStatic libraries are bolted into the executable rather than being somewhere else on the system and loaded into the executable by ld.so. The pro is that the executable is self contained; it doesn't depend on code elsewhere on the system, and not subject to missing libraries. You can give this code to anyone else and it's guarenteed to work. The con is that the executable is larger than necessary and you can't share code between different programs.Static libraries are named, by convention, with the string .a appended to the library name. Some examples are:
III. Comparison Between Dynamic and Static LibrariesPrograms that use static libraries that were bolted into them should be slightly faster than programs that have to use ld.so to load in dynamically linked code. I've seen people say that the order of magnitude of startup time difference is abot 5%. However, it has to be kept in mind that one of the benefits of dyanmic libraries is that in a good case scenario, the code is already loaded into memory. In fact, for libc, this is always going to be true. If you tried to load a statically linked netscape, you'd have to wait consderably longer for it to load. The answer to which one is faster is `it depends'. But all in all, it's a safe bet to say that a program will be faster with dynamic libraries since chances are good that the code you need is already in memory.
IV. Important Files/ExecutablesThe program /lib/ld.so is responsible for linking dynamic libraries for an executable. It searches for library files in the following order:
The file /etc/ld.so.cache contains a compiled list of directories to search for libraries and an ordered list of candidate libraries. It's created by /sbin/ldconfig. The program /sbin/ldconfig is run automatically (/etc/rc) at boot or manually by the user after compiling libraries (it's a good idea to ALWAYS run ldconfig after installing software). ldconfig generates the file /etc/ld.so.cache. It searches /lib, /usr/lib and all the directories listed by /etc/ld.so.conf. The configuration file /etc/ld.so.conf lists all the directories that ldconfig is to search for libraries to generate ld.so.cache. If you ever put library files in a non-standard directory (like /home/lib) you should add that directory to the end of ld.so.conf and rerun ldconfig.
V. Cool UtilitiesMany programs require dynamic libraries to be present on your system so the library's code can be used by the program. How can you find out which dynamic libraries are required by any given program? There's two ways.
You can use strace. For an example, find xmms, xcalc or xpaint on your system
using the locate command. Try the following command:
Fortunately, there's an easier way to find this information. Try:
VI. End NotesSo when you write a hello world program and compile it with gcc, are you using dynanmic or static libraries? gcc uses dynamic libraries, unless you give the -static option to the linker. As an example, here is my hello world program:% cat try.cafter compiling it with: % gcc try.chere is what ldd has to say about a.out: % ldd a.outNow let's compile it with static libraries: % gcc -static try.cand here's what ldd has to say about the new executable: % ldd a.out |