1. Home
  2. Computing & Technology
  3. Focus on Linux

The Linux Gamers' How-To

From The Linux Documentation Project, for About.com

5.3. About libraries on Linux

A common problem you'll see in gaming is a library file not being found. They're kind of mysterious and have funny names, so we'll go over libraries on Linux for a bit. There are two types of libraries, static and dynamic. When you compile a program, by default, gcc uses dynamic libraries, but you can make gcc use static libraries instead by using the -static switch. Unless you plan on compiling your games from source code, you'll mainly be interested in dynamic libraries.

5.3.1. Dynamic libraries

Dynamic libraries, also called a “shared library”, provide object code for an application while it's running. That is, code gets linked into the executable at run time, as opposed to compile time. They're analagous to the .dll 's used by Windows. The program responsible for linking code “on the fly” is called /etc/ld.so , and the dynamic libraries themselves usually end with .so with a version number, like:


   

/usr/lib/libSDL.so
/lib/libm.so.3

When using gcc , you refer to these libraries by shaving off the strings lib , .so and all version numbers. So to use these two libraries, you would pass gcc the -lSDL -lm options. gcc will then 'place a memo inside the executable' that says to look at the files /usr/lib/libSDL.so and /lib/libm.so.3 whenever an SDL or math function is used.

5.3.2. Static libraries

In contrast to dynamic libraries which provide code while the application runs, static libraries contain code which gets linked (inserted) into the program while it's being compiled. No code gets inserted at run time; the code is completely self-contained. Static libraries usually end with .a followed by a version number, like:


   

/usr/lib/libSDL.a
/usr/lib/libm.a

The .a files are really an archive of a bunch of .o (object) files archived together, similar to a tar file. You can use the nm to see what functions a static library contains:


   

% nm /usr/lib/libm.a
...
e_atan2.o:
00000000 T __ieee754_atan2

e_atanh.o:
00000000 T __ieee754_atanh
00000000 r half
00000010 r limit
00000018 r ln2_2
...

When using gcc , you refer to these libraries by shaving off the strings “lib”, “.a” and all version numbers. So to use these two libraries, you would pass gcc the -lSDL -lm options. gcc will then 'bolt on' code from /usr/lib/SDL.a and /usr/lib/libm.a whenever it sees a math function during the compilation process.

5.3.3. How are library files found

If you compile your own games, your biggest problem with libraries will either be that gcc can't find a static library or perhaps the library doesn't exist on your system. When playing games from binary, your library woes will be either be that ld.so can't find the library or the library doesn't exist on your system. So it makes some sense to talk about how gcc and ld.so go about finding libraries in the first place.

gcc looks for libraries in the ''standard system directories'' plus any directories you specify with the -L option. You can find what these standard system directories are with gcc -print-search-dirs

ld.so looks to a binary hash contained in a file named /etc/ld.so.cache for a list of directories that contain available dynamic libraries. Since it contains binary data, you cannot modify this file directly. However, the file is generated from a text file /etc/ld.so.conf which you can edit. This file contains a list of directories that you want ld.so to search for dynamic libraries. If you want to start putting dynamic libraries in /home/joecool/privatelibs ,

* License

* The Linux Gamers' How-To Index

Explore Focus on Linux

More from About.com

  1. Home
  2. Computing & Technology
  3. Focus on Linux
  4. Linux HowTos
  5. The Linux Gamers How-To
  6. The Linux Gamers' How-To - 5.3. About libraries on Linux

©2008 About.com, a part of The New York Times Company.

All rights reserved.