Main
BUILDING STATIC LIBRARY (EXAMPLE)

In this example static library contains two definitions of classes, which are compiled separately:

class1.cpp
class2.cpp
library_header.h
#include"library_header.h"

C1 :: C1() {
  //...
}
int C1 :: fun() {
  //...
}
#include"library_header.h"

C2 :: C2() {
  //...
}
int C2 :: fun() {
  //...
}
class C1 {
  //...
  public:
    C1();
    int fun();
};

class C2 {
  //...
  public:
    C2();
    int fun();
};


1. Compiling two classes (creating static modules):
>g++ -c class1.cpp -o class1.o
>g++ -c class2.cpp -o class2.o

2. Building static library:
>ar -rcs libStaticLibExample.a class1.o class2.o

3. Using classes from library in program:
program.cpp
#include"library_header.h"
// or #include<library_header.h> if header is in default include directory

int main() {
  //...
  C1 a;
  C2 b;
  a.fun();
  b.fun();
}


4. Compiling program.cpp:
>g++ -c program.cpp -o program.o

5. Linking program with static library (if library is in the same folder):
>g++ program.o libStaticLibExample.a -o program.exe

or (if library is in default lib folder):
>g++ program.o -lStaticLibExample -o program.exe


Note that linker links only modules (not whole library), in this example class1.o and class2.o from library, which contains compiled code of classes used in program.
Hosted by www.Geocities.ws

1