Using Borland's Command Line Tools
Page 3: Static Libraries

Installation, First Use Multi-File Programs
Static Libraries Static Linked and Dynamic Linked
Make Files [1],  [2],  [3],  [4]  [Summary] Create and Use a DLL
Resource Files [1]

Zip files containing the code from this page can be found here:
   [C++ Language ZIP File]    [C Language ZIP File]

Static libraries have an extension of .LIB  They contain object files and a table which tells what they contain.  They are convenient because they remove the need to remember the source file name for every function that you use.

The linker assembles the object files you give it, making a list of the items (mostly the public names of functions) that you provide and those which you use. For any that are used but have not been provided it searches the libraries for matching names.  Each object file in the library that has a needed name will be linked into the executable. Object files in the library that do not contain a needed item will not be linked in.

Static libraries are created with the librarian, Tlib.exe.  Assume that you have the object files one.obj, two.obj and three.obj and wish to create a static library named Numbers.LIB which contains them.  A command line to create that library could be:
  tlib /C Numbers -+one-+two-+three,Numbers
The /C says the library should be case-sensitive, necessary for C and C++ languages.

The librarian knows the default extensions for each of the arguments so you do not need to type them in.

The first (leftmost) word Numbers is the name of the library and refers to Numbers.LIB.  If it does not exist, tlib.exe will create it.

The -+ before the names of each object file to put in the library are commands specifying what to do.  The '-' says to delete any existing object file of that name from the library.  The '+' says to add this one to the library.  If you are creating the library for the first time or if the object files are not already in the library it will warn you that it did not find them in there.

The ending (rightmost) word Numbers following the comma is optional.  It is the name of a listing file which shows what is in the library. Its name is Numbers.LST

Static libraries usually contain many object files, too many to fit on a command line.  Tlib is most often used with a response file, a text file from which it will take commands.

Below is an example of a command and file to do the same thing as we did with above:
  tlib /C numbers @numbers.rsp

  -----Contents of Numbers.rsp------
  -+one &
  -+two &
  -+three,numbers
  ----------------------------------
Note: the & in the example above is a line continuation character. It must have a space before it (to its left).

Here is an example of a main program using the library mentioned above.
  // -----123.cpp------
  #include <iostream>
  #include "numbers.h"
  using namespace std;

  int main()
    {
    cout << One() << " "
	 << Two() << " "
	 << Three() << endl;
    return 0;
    }
  // ------one.cpp-------
  int One() { return 1; }
  // ------two.cpp-------
  int Two() { return 2; }
  // ------three.cpp-------
  int Three() { return 3; }
  // ----numbers.h-----
  #ifndef NUMBERS_H
  #define NUMBERS_H

  int One();
  int Two();
  int Three();

  #endif
  // ------------------
  bcc32 -c one two three
  tlib /C numbers @numbers.rsp
  bcc32 -WC 123 numbers.lib
Note that on this page we are discussing a Static Library.  There is another kind of library, an Import Library.  Both have the file extension .LIB but they are very different things.  A Static Library has actual object files containing machine code but an Import library does not.  When you link your program with a static library, that library is said to be Static Linked.
 
[Next Page: Static Linked and Dynamic Linked]

:: Main | Search | Site Map
 
:: Algorithms & Code
:: Assembly & Low Level
:: Borland/CodeGear
:: C & C++
:: CGI
:: CDROM Info
:: COM/DCOM/CORBA
:: Communications
:: Compression
:: Cryptography
:: File Formats
:: General Software
:: Games & Graphics
:: Hardware
:: HTML, Web Development
:: Linux/Unix/BSD
:: Microsoft
:: Miscellaneous
:: Neural Nets
:: New Links
:: Programming Articles
:: Programming Languages
:: Protocols
:: Sfwe Publications
:: Sfwe Sellers
:: Std Template Lib/STL
:: Tutorials, Refs: C/C++
:: Tutorials, Refs: Other
:: Utilities & Tools
:: XML & XHTML
:: Windows, Win32
:: 8051 & Embedded
  Programming Pages 
::  #1 #2 #3 #4 #5 #6 #7
Valid HTML 4.01 Strict