Results 1 to 6 of 6

Thread: g++ : compiling *some* libraries as static, others as dynamic

  1. #1
    Join Date
    May 2007
    Location
    Paris, France
    Beans
    927
    Distro
    Kubuntu 7.04 Feisty Fawn

    g++ : linking *some* libraries as static, others as dynamic

    I'm planning on migrating a PHP web application to a C++ CGI.

    As you may guess, my web host only provides a few shared libraries (libc, libstdc++, ...) so I need to be able to specify which libraries are linked statically, and which ones are linked dynamically.

    I really can't afford a ~900k binary (compiled with -static, then stripped) for a simple "Hello world" CGI...

    Being kinda new to Linux and g++, I'm having trouble configuring the compiler to my needs.

    Any pointers (no pun intended) would be appreciated.
    Last edited by aks44; July 3rd, 2007 at 09:50 PM.

  2. #2
    Join Date
    Jul 2007
    Location
    Southeastern US
    Beans
    11
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: g++ : compiling *some* libraries as static, others as dynamic

    The linker commands allow you to specify libraries to be included and how they are to be included.

    PS

  3. #3
    Join Date
    May 2007
    Location
    Paris, France
    Beans
    927
    Distro
    Kubuntu 7.04 Feisty Fawn

    Re: g++ : compiling *some* libraries as static, others as dynamic

    Thanks PuckStopper31, but would you mind to elaborate on this please?

    I have gone through the linker section of the g++ manual, and didn't found how to solve that problem.

    According to the tests I made (I checked the compiled binary with ldd),

    g++ file.cpp -o out.a -l somelib will dynamically link somelib, but also libc, libstdc++ etc, while

    g++ file.cpp -o out.a -l somelib -static will statically link everything.


    What I want is statically linking somelib, while libc, libstdc++ etc are still dynamically linked.

    Looks like I am missing something obvious...

  4. #4
    Join Date
    Jul 2005
    Beans
    1,535
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: g++ : compiling *some* libraries as static, others as dynamic

    gcc, to my knowledge, will always try and link against the dynamic library even if the static library exists and you ask it to use the static lib.

    The command that you are suppose to use would be something like:
    Code:
    g++ file.cpp -o out  -static -lX11 -dynamic -lm
    which is suppose to statically link libX11 and dynamically link libm, but I'm pretty sure gcc would still link X11 dynamically.

    Good luck.
    When I invented the Web, I didn't have to ask anyone's permission.
    ~Tim Berners-Lee on Net Neutrality
    -------------------------------------
    Visit the Ubuntu Programming IRC-channel at #ubuntu-programming (chat.freenode.net).

  5. #5
    Join Date
    Jun 2007
    Beans
    538

    Re: g++ : compiling *some* libraries as static, others as dynamic

    for older compiler/linkers, the rule was that the command line entries before the
    source were dynamic; after the source only static would be linked. In more
    recent tools, you need to look at the linker used, and its keywords: -Bdynamic -Bstatic
    and others. The keyword governs for all libraries following it. "info ld" should
    give the details.
    Gerald

  6. #6
    Join Date
    May 2007
    Location
    Paris, France
    Beans
    927
    Distro
    Kubuntu 7.04 Feisty Fawn

    Re: g++ : compiling *some* libraries as static, others as dynamic

    Thanks for the answers (and sorry for the late reply).

    For the record, here's the command line that does what I wanted :

    Code:
    g++ file.cpp -o out -Wl,-Bstatic -lsomestaticlib -Wl,-Bdynamic
    The last -Wl,-Bdynamic switch is needed to avoid linker errors on the shared system libraries (libc, libgcc_s, libstdc++, ...).

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •