Creating and linking against static library - Unix

This is a discussion on Creating and linking against static library - Unix ; In folder LIB, I have the files mylibrary.cpp, mylibrary.o, and libmylibrary.a In my code, I include . The code compiles fine, but I can't link against 'libmylibrary.a'. If I run "g++ *.o LIB/ mylibrary.o", everything works fine. If I run ...

+ Reply to Thread
Results 1 to 5 of 5

Thread: Creating and linking against static library

  1. Creating and linking against static library

    In folder LIB, I have the files mylibrary.cpp, mylibrary.o, and
    libmylibrary.a

    In my code, I include . The code compiles fine, but I
    can't link against 'libmylibrary.a'. If I run "g++ *.o LIB/
    mylibrary.o", everything works fine. If I run "g++ -LLIB -lmylibrary
    *.o", the linker seems to find libmylibrary.a, since I don't get an
    error about it, but I get errors for every function in mylibrary.cpp.
    I created libmylibrary.a by running "ar rcs libmylibrary.a
    mylibrary.o", and I don't even know where the problem could be. My
    flags for compiling mylibrary are "-Wall -c -O2 -march=nocona -
    mfpmath=sse -mmmx -msse -msse2 -m64", plus a few -I's and -D's (for
    the OTL library). I can't imagine any of those causing a problem.

  2. Re: Creating and linking against static library

    jbo5112 writes:

    > In folder LIB, I have the files mylibrary.cpp, mylibrary.o, and
    > libmylibrary.a
    >
    > In my code, I include . The code compiles fine, but I
    > can't link against 'libmylibrary.a'. If I run "g++ *.o LIB/
    > mylibrary.o", everything works fine. If I run "g++ -LLIB -lmylibrary
    > *.o", the linker seems to find libmylibrary.a, since I don't get an
    > error about it, but I get errors for every function in mylibrary.cpp.
    > I created libmylibrary.a by running "ar rcs libmylibrary.a
    > mylibrary.o", and I don't even know where the problem could be. My
    > flags for compiling mylibrary are "-Wall -c -O2 -march=nocona -
    > mfpmath=sse -mmmx -msse -msse2 -m64", plus a few -I's and -D's (for
    > the OTL library). I can't imagine any of those causing a problem.


    Short answer: try

    g++ -LLIB *.o -lmylibrary

    Long answer: a static library is really just a bunch of .o files stuck
    together in a fairly simple way (indeed, ar(1) started life as a
    generic file archiver, and is still sometimes used for that purpose).
    When you link with a static library, the linker examines each object
    files it contains and, if it defines symbols that are referenced but
    not yet defined, it pulls in that object. Other objects are not
    pulled in. That way, when you link with libc.a, you don't get a
    megabyte of unnecessary code if you only called strcpy().

    This scanning is done in a one-pass manner by default. Thus, when you
    specify -lmylibrary before any other object files, when the linker
    examines it, no symbols have been referenced yet, so it sees no need
    to pull in any object contained in libmylibrary.a.

    When you explicitly specify a .o file to ld, it unconditionally pulls
    it into the link, whether it has been referenced or not. This
    explains why it works when you do that.

    Note this also means that if you have libraries that reference
    functions in one another, you will need to be careful with the order
    in which they're specified. Alternatively, I seem to recall that GNU
    ld has options ( -( and -) maybe?) to force a group of libraries to be
    scanned repeatedly.

  3. Re: Creating and linking against static library

    Thanks, that worked. I thought I had tried it, but I guess not. If I
    had been intelligent enough to read the man page on ld, instead of
    just gcc and ar, I might have found that myself, but I would have
    missed out on an excellent explanation.

    According to the ld man page, you're also right about the ld options "-
    (" and "-)", but I still get the same errors as before about the
    undefined references from mylibrary, even if I'm using ld directly and
    use the long version of the options. Since I'm already waiting longer
    than I want for the programs to compile and it says there is a
    performance impact, I'm not too concerned about it until I start using
    more libraries than I can easily sort out and get a significantly
    faster development machine.

  4. Re: Creating and linking against static library

    jbo5112 wrote:
    > According to the ld man page, you're also right about the ld options "-
    > (" and "-)", but I still get the same errors as before about the
    > undefined references from mylibrary, even if I'm using ld directly and
    > use the long version of the options. Since I'm already waiting longer
    > than I want for the programs to compile and it says there is a
    > performance impact, I'm not too concerned about it until I start using
    > more libraries than I can easily sort out and get a significantly
    > faster development machine.


    There's a reason most linkers don't have this option, which is that it's
    very rarely needed. It's generally possible, as you found, to order the
    link line such that everything is found and in a pinch, a solution which
    is both faster and more portable than the GNU extensions is to simply
    list the required libraries twice, e.g. "... -laaa -lbbb -laaa ...".
    Bottom line, don't go looking for reasons to use the parenthesis options
    and you will probably never need them.

    AS

  5. Re: Creating and linking against static library

    Arch Stanton writes:

    > There's a reason most linkers don't have this option, which is that
    > it's very rarely needed. It's generally possible, as you found, to
    > order the link line such that everything is found


    That's only because generally people structure their libraries in
    a sane (layered) way.

    > and in a pinch, a
    > solution which is both faster and more portable than the GNU
    > extensions is to simply list the required libraries twice,


    Twice may not be enough.

    It is easy to construct a (artificial) case where each library
    would need to be repeated N times (for any value of N).

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

+ Reply to Thread