Search code examples
cstatic-libraries

Pulling Function definitions out of a static C library's source code to avoid library compilation


I have a program that uses a huge library, call it libhuge (compilation takes a long time). My Makefile first compiles libhuge and archives it. Then compiles my programs source code into object files gcc -c $CFLAGS $OBJS

Then links them all to libhuge.a. I tried compiling without linking to that library and saw there were very few (12 functions) that were missing definitions. Is it possible to automatically pull those definitions from my libhuge, and compile only those functions in an object file so I won't have to compile the whole library?

Let's put it this way! I have some functions I would like to further look into from a library. How do I find their definitions?


Solution

  • Add a rule in your Makefile that checks for the libhuge.a and only if it doesn't exist then build the libhuge.a library:

    <path>/libhuge.a:
        <commands to build libhuge.a>
    
    <your program>: <path>/libhuge.a
         <commands to build your code>
    

    The first time when libhuge.a does not exist, it will build the entire libhuge.a. But once the library exists, it won't build it again.