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

StackOverflow https://stackoverflow.com/questions/22367107

  •  13-06-2023
  •  | 
  •  

Question

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?

Was it helpful?

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top