Creating libraries - Linux

This is a discussion on Creating libraries - Linux ; Real simple (this is for a much larger, complicated situation) but I've got to understand the basics: I've set up a real simple directory with three files: t1.c, t2.c, and t3.c. I've compiled all three like this: gcc -c t1.c ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: Creating libraries

  1. Creating libraries

    Real simple (this is for a much larger, complicated situation) but
    I've got to understand the basics:

    I've set up a real simple directory with three files: t1.c, t2.c, and
    t3.c.
    I've compiled all three like this:
    gcc -c t1.c
    gcc -c t2.c
    gcc -c t3.c

    and the files, t1.o, t2.o, and t3.o are created.

    Then I do:
    ar rc lib1.a t1.o
    ar rc lib2.a t2.o
    ar rc lib3.a t3.o

    then:
    ranlib lib1.a
    ranlib lib2.a
    ranlib lib3.a

    t1.c looks like this:

    void add1(int *x)
    {
    *x++;
    }

    void sub1(int *x)
    {
    *x--;
    }



    Now when I compile t1.c (only need lib1.a for now) it always complains
    that there's no add1.

    I could show you all of the variants of the gcc command line that I
    tried, but they all say the same.

    How should I compile this (and what options) so that it picks up
    lib1.a?

    Thanks,

    Scott


    gcc


  2. Re: Creating libraries

    > Now when I compile t1.c (only need lib1.a for now) it always complains
    > that there's no add1.

    I guess you need to publish the functions by header-files.
    t1.h could look as this:

    #ifndef define _T1_H
    #define _T1_H
    void add1(int *x);
    #endif

    Of course you have to include the header on top in your c-file:
    #include "t1.h"

    At least in g++ it would be sufficient to call the compiler with -static
    to build static libs...

    Greetings johannes

  3. Re: Creating libraries

    ScottReeve wrote:
    > Real simple (this is for a much larger, complicated situation) but
    > I've got to understand the basics:
    >
    > I've set up a real simple directory with three files: t1.c, t2.c, and
    > t3.c.
    > I've compiled all three like this:
    > gcc -c t1.c
    > gcc -c t2.c
    > gcc -c t3.c
    >
    > and the files, t1.o, t2.o, and t3.o are created.
    >
    > Then I do:
    > ar rc lib1.a t1.o
    > ar rc lib2.a t2.o
    > ar rc lib3.a t3.o
    >
    > then:
    > ranlib lib1.a
    > ranlib lib2.a
    > ranlib lib3.a
    >
    > t1.c looks like this:
    >
    > void add1(int *x)
    > {
    > *x++;
    > }
    >
    > void sub1(int *x)
    > {
    > *x--;
    > }
    >
    >
    >
    > Now when I compile t1.c (only need lib1.a for now) it always complains
    > that there's no add1.


    ??? Above you write "I've compiled all three like this: gcc -c t1.c ...
    and the files, t1.o, ... are created."
    Now you say you can't compile t1.c?


    > I could show you all of the variants of the gcc command line that I
    > tried, but they all say the same.
    >
    > How should I compile this (and what options) so that it picks up
    > lib1.a?


    If you have a program, say app1.c, that calls add1():

    void add1(int *);

    int main(int argc, char *argv[])
    {
    int i;

    add1(&i);
    }

    Then you'd compile that with

    gcc app1.c lib1.a -o app1

    The compiler will see that lib1.a is not a C source file and pass it on
    to the linker who will extract whatever object he needs, t1.o in this
    case, and link it to the (intermediate) app1.o.

    --
    These are my personal views and not those of Fujitsu Siemens Computers!
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize (T. Pratchett)
    Company Details: http://www.fujitsu-siemens.com/imprint.html