Adding an object file to an existing library - Unix

This is a discussion on Adding an object file to an existing library - Unix ; Assuming you have an existing static library (*.a) under linux, hpux, aix and mac osx I would like to add another object file into it. Under linux I tried do: ar cr mynewlib.a existing.a file2.o This resulted in a new ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: Adding an object file to an existing library

  1. Adding an object file to an existing library

    Assuming you have an existing static library (*.a) under linux, hpux, aix
    and mac osx I would like to add another object file into it.

    Under linux I tried do:

    ar cr mynewlib.a existing.a file2.o

    This resulted in a new library but when trying to link with it I get missing
    symbols from the existing.a library. Do I need to extract the library before
    adding it in again?

    Thanks.

    -- Henrik


  2. Re: Adding an object file to an existing library

    >Assuming you have an existing static library (*.a) under linux, hpux, aix
    >and mac osx I would like to add another object file into it.
    >
    >Under linux I tried do:
    >
    >ar cr mynewlib.a existing.a file2.o


    ar c ('create') wipes out the existing library and creates a new
    one with the files you requested.

    ar r ('replace') replaces (or adds new modules) modules in an
    existing library, and is probably what you meant to do.



  3. Re: Adding an object file to an existing library

    "Henrik Goldman" writes:

    > ar cr mynewlib.a existing.a file2.o


    This create mynewlib.a, containing two members: existing.a and
    file2.o (use "ar tv mynewlib.a" to observe that).

    The linker only searches archive libraries one level deep (IOW,
    it only sees the one file2.o object in mynewlib.a).

    > This resulted in a new library but when trying to link with it I get
    > missing symbols from the existing.a library. Do I need to extract the
    > library before adding it in again?


    Yes:

    mkdir tmp.$$ && cd tmp.$$ && ar x ../existing.a &&
    ar c ../mynewlib.a *.o ../file2.o && cd .. && rm -rf tmp.$$

    Better yet, 'ar r existing.a file2.o' to "append" the new object
    to existing.a, or (if you don't want to modify existing.a)
    cp -p existing.a mynewlib.a && ar r mynewlib.a file2.o

    Cheers,
    --
    In order to understand recursion you must first understand recursion.
    Remove /-nsp/ for email.

  4. Re: Adding an object file to an existing library

    Gordon Burditt wrote:

    >>Under linux I tried do:
    >>
    >>ar cr mynewlib.a existing.a file2.o

    >
    > ar c ('create') wipes out the existing library and creates a new
    > one with the files you requested.


    No it doesn't. All the 'c' option does is suppress the warning
    message that "ar -r" writes when it creates an archive.

    > ar r ('replace') replaces (or adds new modules) modules in an
    > existing library, and is probably what you meant to do.


    What he wanted to do was:

    cp existing.a mynewlib.a
    ar -r mynewlib.a file2.o

    (Note the '-': using "ar r" is non-standard, "ar -r" is the usage
    specified by POSIX.)

    --
    Geoff Clare

+ Reply to Thread