how to link some libs as static and some as shared to one app? - Linux

This is a discussion on how to link some libs as static and some as shared to one app? - Linux ; Hi everyone. I hava a simple question. Consider such a situation. My program uses 2 libs: lib1 and lib2, that located in /lib. Both this libs has static and shared versions: lib1.a, lib1.so and lib2.a, lib2.so. The question is which ...

+ Reply to Thread
Results 1 to 11 of 11

Thread: how to link some libs as static and some as shared to one app?

  1. how to link some libs as static and some as shared to one app?

    Hi everyone.

    I hava a simple question. Consider such a situation. My program uses 2
    libs: lib1 and lib2, that located in /lib. Both this libs has static
    and shared versions: lib1.a, lib1.so and lib2.a, lib2.so. The question
    is which option should I pass to gcc to force it link lib1 as a static
    and lib2 as a shared to my program?

    Thanks in advance.


  2. Re: how to link some libs as static and some as shared to one app?

    Dmitry Chumack wrote:
    > Hi everyone.
    >
    > I hava a simple question. Consider such a situation. My program uses 2
    > libs: lib1 and lib2, that located in /lib. Both this libs has static
    > and shared versions: lib1.a, lib1.so and lib2.a, lib2.so. The question
    > is which option should I pass to gcc to force it link lib1 as a static
    > and lib2 as a shared to my program?


    gcc foo.o -o foo -Wl,-static -lbaz -lkaz -Wl,-Bdynamic -lkaz
    links in libbar libkaz(and system libraries such as libc) dynamic,
    while libbaz and libkaz are linked statically.

    Or you specify the path to the static archives.
    gcc foo.o -o foo -lbar /usr/lib/libbaz.a /usr/lib/libkaz.a -lkaz

  3. Re: how to link some libs as static and some as shared to one app?

    "Nils O. Selåsdal" writes:

    >> which option should I pass to gcc to force it link lib1 as a static
    >> and lib2 as a shared to my program?

    >
    > gcc foo.o -o foo -Wl,-static -lbaz -lkaz -Wl,-Bdynamic -lkaz


    That answer is incorrect (it forces linker to perform "completely
    static" link; then half-way through the link makes it change its
    mind; doing so will likely cause all kinds of linkage problems for
    non-trivial executables).

    Correct answer is:

    gcc foo.o -o foo -Wl,-Bstatic -l1 -Wl,-Bdynamic -l2

    > Or you specify the path to the static archives.


    That works.

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

  4. Re: how to link some libs as static and some as shared to one app?

    Paul Pluzhnikov wrote:
    > "Nils O. Selåsdal" writes:
    >
    >>> which option should I pass to gcc to force it link lib1 as a static
    >>> and lib2 as a shared to my program?

    >> gcc foo.o -o foo -Wl,-static -lbaz -lkaz -Wl,-Bdynamic -lkaz

    >
    > That answer is incorrect (it forces linker to perform "completely
    > static" link; then half-way through the link makes it change its
    > mind; doing so will likely cause all kinds of linkage problems for
    > non-trivial executables).
    >
    > Correct answer is:
    >
    > gcc foo.o -o foo -Wl,-Bstatic -l1 -Wl,-Bdynamic -l2
    >


    Could you elaborate further on what kind of problems, I've used this
    approach without apparent problems on many "non-trivial" projects.
    And - atlest my ld(1) documentation says -static and -Bstatic are synonyms.

  5. Re: how to link some libs as static and some as shared to one app?

    Nils O. Selåsdal wrote:
    > Paul Pluzhnikov wrote:
    >
    >> "Nils O. Selåsdal" writes:
    >>
    >>>> which option should I pass to gcc to force it link lib1 as a static
    >>>> and lib2 as a shared to my program?
    >>>
    >>> gcc foo.o -o foo -Wl,-static -lbaz -lkaz -Wl,-Bdynamic -lkaz

    >>
    >>
    >> That answer is incorrect (it forces linker to perform "completely
    >> static" link; then half-way through the link makes it change its
    >> mind; doing so will likely cause all kinds of linkage problems for
    >> non-trivial executables).
    >>
    >> Correct answer is:
    >>
    >> gcc foo.o -o foo -Wl,-Bstatic -l1 -Wl,-Bdynamic -l2
    >>

    >
    > Could you elaborate further on what kind of problems, I've used this
    > approach without apparent problems on many "non-trivial" projects.
    > And - atlest my ld(1) documentation says -static and -Bstatic are
    > synonyms.


    On my system (Fedora core 3, gcc 3.4.4) man ld has:

    -Bstatic
    -dn
    -non_shared
    -static
    Do not link against shared libraries. This is only
    meaningful on platforms for which shared libraries
    are supported. The different variants of this option are for
    compatibility with various systems. You may use this option multiple
    times on the command line: it affects library searching for -l options
    which follow it. This option also implies --unresolved-symbols=report-all.


    Which means to me that:
    gcc foo.o -o foo -Wl,-Bstatic -l1 -Wl,-Bdynamic -l2
    and:
    gcc foo.o -o foo -Wl,-static -l1 -Wl,-Bdynamic -l2
    would be the same. There seems to be no corresponding -dynamic, however.

  6. Re: how to link some libs as static and some as shared to one app?

    Tim Keck writes:

    > On my system (Fedora core 3, gcc 3.4.4) man ld has:
    >
    > -Bstatic
    > -static
    > Do not link against shared libraries


    My mistake: I always thought '-static' meant completely static link,
    but apparently it doesn't.

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

  7. Re: how to link some libs as static and some as shared to one app?

    Paul Pluzhnikov wrote:
    > Tim Keck writes:
    >
    >> On my system (Fedora core 3, gcc 3.4.4) man ld has:
    >>
    >> -Bstatic
    >> -static
    >> Do not link against shared libraries

    >
    > My mistake: I always thought '-static' meant completely static link,
    > but apparently it doesn't.


    There is perhaps a difference if you give -static to gcc, and not
    directly to the linker. It's not apparent what that difference is
    though :-\

  8. Re: how to link some libs as static and some as shared to one app?


    Dmitry Chumack wrote:

    > I hava a simple question. Consider such a situation. My program uses 2
    > libs: lib1 and lib2, that located in /lib. Both this libs has static
    > and shared versions: lib1.a, lib1.so and lib2.a, lib2.so. The question
    > is which option should I pass to gcc to force it link lib1 as a static
    > and lib2 as a shared to my program?
    >
    > Thanks in advance.


    I should caution you that there are many cases where combined static
    and dynamic linking won't work, won't work right, or will have really
    strange side-effects. The most common problem is when one of the
    libraries statically linked to is itself statically linked to one of
    the libraries that one of the dynamically-linked libraries is
    dynamically-linked to.

    For example, a static library will typically be statically-linked to
    libc. However, a shared library will typically be dynamically-linked to
    libc. So when you use both libraries, one is statically-linked to the
    libc it was compiled with and one is dynamically-linked to the libc it
    found at runtime. If they have, say, different 'malloc'
    implementations, very bad things will happen when memory is allocated
    by one and freed by the other.

    Lots of other things can go wrong. I don't recommend such a hybrid
    unless you really know exactly what you're doing or the result will not
    leave the system on which it was made.

    An update of a dynamically-linked library that one of the
    statically-linked libraries uses could break such a program, even if
    the versions should normally be compatible.

    DS


  9. Re: how to link some libs as static and some as shared to one app?

    David Schwartz wrote:
    > Dmitry Chumack wrote:
    >
    >> I hava a simple question. Consider such a situation. My program uses 2
    >> libs: lib1 and lib2, that located in /lib. Both this libs has static
    >> and shared versions: lib1.a, lib1.so and lib2.a, lib2.so. The question
    >> is which option should I pass to gcc to force it link lib1 as a static
    >> and lib2 as a shared to my program?
    >>
    >> Thanks in advance.

    >
    > I should caution you that there are many cases where combined static
    > and dynamic linking won't work, won't work right, or will have really
    > strange side-effects. The most common problem is when one of the
    > libraries statically linked to is itself statically linked to one of
    > the libraries that one of the dynamically-linked libraries is
    > dynamically-linked to.


    Static libraries, being just a collection of the .o files, are not
    statically linked to other libraries. I don't see how you produce
    a static library that is linked to a static libc.

    More common problems is linking your entire program statically - linking
    glibc statically can break certain features.
    And beware when creating shared libraries that includes a static library
    (Make sure that static library was compiled with PIC !)


  10. Re: how to link some libs as static and some as shared to one app?

    "Nils O. Selåsdal" writes:

    > There is perhaps a difference if you give -static to gcc, and not
    > directly to the linker.


    Indeed.

    > It's not apparent what that difference is though :-\


    Reading 'specs' reveals several significant differences,
    from not using '-dynamic-linker ...' to linking with
    different crtbegin*.o to using different libgcc*.

    Here is a complete diff:

    $ gcc -v junk.o 2>&1 | fmt -1 > junk.nonstatic
    $ gcc -v junk.o 2>&1 -static | fmt -1 > junk.static
    $ diff junk.*static | egrep '<|>'
    < --eh-frame-hdr
    < -dynamic-linker
    < /lib/ld-linux.so.2
    > -static

    < /usr/lib/gcc-lib/i386-redhat-linux/3.3.3/crtbegin.o
    > /usr/lib/gcc-lib/i386-redhat-linux/3.3.3/crtbeginT.o
    > --start-group

    < --as-needed
    < -lgcc_s
    < --no-as-needed
    > -lgcc_eh

    < -lgcc
    < --as-needed
    < -lgcc_s
    < --no-as-needed
    > --end-group



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

  11. Re: how to link some libs as static and some as shared to one app?


    Nils O. Selåsdal wrote:

    > Static libraries, being just a collection of the .o files, are not
    > statically linked to other libraries. I don't see how you produce
    > a static library that is linked to a static libc.


    What I mean is that when you link the executable, you link it to, say
    'foo.a' and 'libc.a', and as a result, 'foo.a's accesses to functions
    like 'malloc' resolve to accesses of the 'malloc' provided in 'libc.a'.

    DS


+ Reply to Thread