Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: g++ and static libraries / use .a instead of .so

  1. #1
    Join Date
    Jun 2005
    Location
    Norway
    Beans
    57
    Distro
    Kubuntu 9.04 Jaunty Jackalope

    Question g++ and static libraries / use .a instead of .so

    Hi. I have a minor problem. Do you know how to make g++ -l flags use the .a library instead of the .so ones when both are available ? Hardcoding the path works fine, but I assume there is a better way.

    In short; if you have liba.so and liba.a and specify -la to g++ it seems to pick up liba.so. Can you make it prefer .a instead ?

  2. #2
    Join Date
    Apr 2007
    Beans
    14,781

    Re: g++ and static libraries / use .a instead of .so

    Quote Originally Posted by Cygnus View Post
    Hi. I have a minor problem. Do you know how to make g++ -l flags use the .a library instead of the .so ones when both are available ? Hardcoding the path works fine, but I assume there is a better way.

    In short; if you have liba.so and liba.a and specify -la to g++ it seems to pick up liba.so. Can you make it prefer .a instead ?
    .a is for static linking and .so is for shared libraries.

    Look up on how to static link (man gcc)

  3. #3
    Join Date
    Jun 2005
    Location
    Norway
    Beans
    57
    Distro
    Kubuntu 9.04 Jaunty Jackalope

    Re: g++ and static libraries / use .a instead of .so

    Quote Originally Posted by LaRoza View Post
    .a is for static linking and .so is for shared libraries.

    Look up on how to static link (man gcc)
    The static linking works fine when using full path to the .a file, I just want to know if there is a flag or setting to make use static linking of a library when you use -l and there are are both .so and .a versions available.

    I have already looked in man gcc of course but not yet found anything.

  4. #4
    Join Date
    Aug 2007
    Location
    Canada
    Beans
    459
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: g++ and static libraries / use .a instead of .so

    If the library name is liba.a, you should get the linking adding the flag -static -la.

    EDITED
    Last edited by gnusci; July 8th, 2008 at 12:27 AM.
    Best, Gnusci

    "Never make a calculation until you know the answer." -- Wheeler, Spacetime Physics, pg 60.

  5. #5
    Join Date
    Mar 2006
    Beans
    199

    Re: g++ and static libraries / use .a instead of .so

    pass the option -Wl,-Bstatic to g++,

    PHP Code:
    g++ [your other stuff] -Wl,-Bstatic -la 

  6. #6
    Join Date
    Jun 2005
    Location
    Norway
    Beans
    57
    Distro
    Kubuntu 9.04 Jaunty Jackalope

    Smile Re: g++ and static libraries / use .a instead of .so

    Thanks for the replies. I had earlier tried with the -static flag but it only gave me errors. I now continued to investigate that and I found that this combination worked:

    Code:
    -static-libgcc -Wl,-static -la
    -static must be passed to the linker as supirman suggested with -Wl but I also needed -static-libgcc or I ran into the error described at this link: <debian-gcc>.

  7. #7
    Join Date
    Apr 2007
    Location
    (X,Y,Z) = (0,0,0)
    Beans
    3,715

    Re: g++ and static libraries / use .a instead of .so

    But, wait: Won't -static static-link all libraries? If you want to static-link only some libraries, probably using the whole path to the .a file is the best solution (remember: A .a file is just a bunch of .o files, so both are linked the same way).

  8. #8
    Join Date
    Jun 2005
    Location
    Norway
    Beans
    57
    Distro
    Kubuntu 9.04 Jaunty Jackalope

    Re: g++ and static libraries / use .a instead of .so

    Quote Originally Posted by nvteighen View Post
    But, wait: Won't -static static-link all libraries? If you want to static-link only some libraries, probably using the whole path to the .a file is the best solution (remember: A .a file is just a bunch of .o files, so both are linked the same way).
    Note that -Wl,-static goes to the linker. It only affects libraries after it. From man ld:

    You may use this option multiple times on the command line: it affects library searching for -l options which follow it.

  9. #9
    Join Date
    Jun 2006
    Location
    Uruguay
    Beans
    35
    Distro
    Kubuntu 7.04 Feisty Fawn

    Re: g++ and static libraries / use .a instead of .so

    isn't there any elegant way to say to the compiler: "prefer static libraries"? If you are linking against dozens of libraries or you don't know (nor want to find out) which ones are static or shared, using the order to specify it really is not an option.
    No one knows any workaround for this?

  10. #10
    Join Date
    Jul 2008
    Location
    Dublin, Ireland
    Beans
    633
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: g++ and static libraries / use .a instead of .so

    Quote Originally Posted by Cygnus View Post
    Hi. I have a minor problem. Do you know how to make g++ -l flags use the .a library instead of the .so ones when both are available ? Hardcoding the path works fine, but I assume there is a better way.

    In short; if you have liba.so and liba.a and specify -la to g++ it seems to pick up liba.so. Can you make it prefer .a instead ?
    Your question has already been answered, just a hint: be careful with static library linking, as they are dependent on the order in which you type the link/compiler command line. An static library can depend only on symbols defined previously in the command line.

    David

Page 1 of 2 12 LastLast

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
  •