Thread: GCC: Compiling with both static and shared libraries

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    9

    GCC: Compiling with both static and shared libraries

    I apologize if this has been asked before; I searched the forum and didn't find anything.

    Is it possible to compile and link a program using GCC such that some of the libraries are linked in as static libraries while the rest are shared?

    For example, I have a program, foo, that uses the socket and nsl libraries, but also uses a custom library that won't be available on all systems. I want to link libsocket and libnsl as shared libraries, but the custom one as static.

    Can this be done? If so, what is the command line option in GCC for doing so? Thanks in advance.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Just add the static version of the custom library on the command-line of the linking stage of your build.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    9
    Odd. I tried that, but linking it the .a file produces a bunch of undefined symbol errors. Linking in the .so works fine, but isn't that still dynamic linking at that point?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by eatwithaspork View Post
    Odd. I tried that, but linking it the .a file produces a bunch of undefined symbol errors. Linking in the .so works fine, but isn't that still dynamic linking at that point?
    'tis. Strange. Which library do those references the .a "needs" belong to?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    9
    Found the problem. The library I'm trying to link statically is libtiff. I didn't phase me that libtiff could have a bunch of its own dependencies, which it does.

    Guess it's time to track those down and compile them as well. Thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. shared library, but not dynamic !?
    By ccox421 in forum C Programming
    Replies: 11
    Last Post: 03-17-2009, 01:03 PM
  3. gcc compiling and linking problem
    By maven in forum C Programming
    Replies: 6
    Last Post: 11-14-2008, 05:28 AM
  4. Difference between static DLL and Shared DLL
    By Bargi in forum Windows Programming
    Replies: 1
    Last Post: 07-04-2007, 02:03 AM
  5. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM