Thread: static library linking

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    63

    static library linking

    I am trying to refresh my memory here as I did some studies many years ago but the results elude me.
    Also todays c/c++ compilers may have better optimizations.

    Say I have a static library that includes three obj modules.

    Each of these object modules has a number of functions. These functions do not reference any other functions within the obj module.

    My main app links this library but only references one function from each of the object modules.

    Question:
    Are the complete contents of each module linked into my main app or are todays linkers smart enough to just link
    the functions used?

    James

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    That would depend entirely on your linker (often bundled with compiler). And depending on your linker and the executable format you produce, you may have a variety of tools to examine the resulting executable and see just what was bundled into that. For example, on Linux, you can use objdump to examine the contents of object files and ELF binaries, just compile with debug symbols on. Try compiling with different linker options and see how things differ.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Specifically, for the GNU utilities (gcc compiler, ar archiver for static libraries and ld linker), you have resolution down to the object file. That is, the linker only pulls in .o files from your static library if it needs something from that object file. But it does pull in the entire .o file. Thus, if you had a .c file containing
    Code:
    #include <stdio.h>
    
    void one_a(void)
    {
        printf("one_a");
    }
    
    void one_b(void)
    {
        printf("one_b");
    }
    
    void one_c(void)
    {
        printf("one_c");
    }
    Complied into a .o that was included in a library like libone.a, the linker has to pull in all the functions, even if you only ever call one_a(). But, if each function is in it's own .c file since there's no interdependence, and compiled into it.s own .o file before archiving, then the linker would only pull in the necessary .o files, and thus only the functions you need.

    I don't know how other linkers work. In theory though, a smart linker could do as you ask, and only pull in exactly what it needed, regardless of how many .c/.o files they're in -- it's probably just a little more complicated for the linker that way.

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    63
    Thank you.
    That is the way I remember it.
    I use MinGW in a number of flavors on windows and the gnu utilities on Linux.
    I had not tested in ten years so I was wondering if any advances had been made to the tool chain to make linking a bit smarter.

    James

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    With gcc you can get the unused functions to be stripped off if you compile the object files with the -fdata-sections -ffunction-sections options. Then when you go to make your final executable you link it with -Wl,--gc-sections

    As an example suppose utilities.c contains the functions above from anduril's example, utilities.h is a header file that declares those function prototypes and main.c contains the following code which references only one of the three library functions:

    Code:
    #include "utilities.h"
    
    int main()
    {
        one_b();
    }
    Compile utilities.o as follows. This puts one_a, one_b and one_c into utilities.o but marks them in such a way that they can later be removed by the linker on a per-function basis

    Code:
    gcc -fdata-sections -ffunction-sections -o utilities.o -c utilities.c
    Then compile main as follows

    Code:
    gcc -o main main.c utilities.o -Wl,--gc-sections
    If you list the contents you will see only one_b is included

    Code:
    $ nm -Cg main
    ...
    0000000000400470 T main
    000000000040056d T one_b
    ...
    For some MinGW versions this will not work when you produce windows executables. To get it to work though you can patch the linker with the instructions here: https://sourceware.org/bugzilla/show_bug.cgi?id=11539

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. User-Defined Static Library Not Linking Against Program
    By QuadraticFighte in forum C Programming
    Replies: 2
    Last Post: 10-15-2010, 02:10 PM
  2. LNK2019 when linking against a static library.
    By g4j31a5 in forum C++ Programming
    Replies: 4
    Last Post: 06-29-2010, 01:39 PM
  3. Help building/linking to static library
    By Kernel Sanders in forum C++ Programming
    Replies: 19
    Last Post: 08-17-2008, 04:35 PM
  4. Replies: 4
    Last Post: 07-06-2006, 02:53 AM
  5. Replies: 19
    Last Post: 01-12-2006, 11:04 AM