delorie.com/djgpp/doc/ug/larger/archives.html   search  
Guide: Making and Using Libraries

Translations: Belorussian

A library is a collection of objects, much like your town library is a collection of books. When building your program, you can make one or more libraries available to gcc and it will use the objects in those libraries to help it complete your program. For example, all the standard C functions (like printf and exit) are in the C library, which is lib/libc.a in a DJGPP installation. When you link your program, gcc adds whatever objects in C library it needs, based on what your program calls. It doesn't link them all in, though, which would be a waste of time and space.

To make your own library, you must first compile each of your sources into an object.

Next, you use the ar command to create the library and put the objects in it.

Each of the rvs letters means something. r means to replace objects in the library with new ones on the command line. Since the library doesn't contain any objects, this effectively means add them to the library. The next time you run it, it will replace the old version with the new version. There are also options for extracting and deleting objects in the library. The v option means verbose, which tells ar to keep you informed about what it's doing. The s option tells ar to create a symbol table, which is something extra that gcc needs when using a library. You could use ar to make libraries of anything - text, images, sounds, etc. When you use ar to make libraries of objects, use the s option.

To use the library, simply add it to your gcc link line just like you would any other object.

It's very important to list libraries in the correct order. As gcc scans the command line, it pulls in what it needs so far. Each time it sees an object, it adds it to the program. Each time it sees a library, it pulls in only the objects that it needs at that point. You should always list libraries after the objects.

Another way to use a library is to give it a special name, put it in a special place, and use a special option for it. Usually, this is used only for the system libraries, because then you don't have to worry about where they are - gcc knows where to find them. If you are building a system library (like the C library, libc.a), you need to do three things.

  1. The name of your library must start with lib, like libc.a or libstuff.a. This extra prefix is a convention that designates your file as a system library.
  2. Your library must be moved into djgpp's lib directory. For example, lib/libc.a and lib/libm.a are in there. gcc knows to look in djgpp's lib directory for system libraries.
  3. When you link your program, you use the -l option to specify only the middle part of your library. For example, if your library is libstuff.a you would use gcc -lstuff to link your library (listing -lstuff after the objects, of course).

  webmaster     delorie software   privacy  
  Copyright © 2011     Updated May 2011