Search code examples
c++static-librarieslibtool

Is there a better way to combine static libraries without carrying a bunch of unnecessary code?


As I saw here (Combine static libraries) I may combine more than one static library using libtool

libtool -static -o new.a old1.a old2.a

As far as I know, this will concatenate every single function from the old libraries to the new one. But what I really want are the functions from the new.a library, the others are there for dependency purpose. Is there a way to combine only the part required by the new.a from the other libraries without carrying a bunch of unnecessary code?


Solution

  • You can extract from the old libraries those object files you wish to incorporate in the new. But there really isn't much point in worrying about it; the linker will only link those object files that are necessary, unlike a shared library where all the symbols defined in the shared library are available to the executable (not that it uses them all, usually).

    The old-fashioned way to do the job would be:

    mkdir new
    cd new
    ar x ../old1.a
    ar x ../old2.a
    ar rv ../new.a *.o
    cd ..
    rm -fr new
    

    After the two x operations, you can weed and whittle the object files to keep what you want for use in new.a.