Linking static lib with other static libs

679 views Asked by At

I'm writing a static lib that needs to handle XML. I want it to include everything that is necessary out of the box but I don't know how to make other static libs link to it, namely libxml2.

I pass the -c flag to gcc to generate .o that ar then uses to create the lib. What magic flag combination must I pass to have my static lib swallow libxml2?

3

There are 3 answers

0
Jeff On

Manually merge the objects into a new static library instance:

ar -x libx.a
ar -x liby.a
ar -c libz.a  *.o
0
Ben On

libraries don't just swallow other libraries. there's two solutions to do what you want that i can think of.

  1. link with both libraries in the final target specifying the new library first.
  2. copy the libxml2 library and just use the lib tool to add your objects to it.
6
Jonathan Mee On

Sadly, what's been most effective for me is to extract all the .os from the static library and then include them with your .os to make a single .a

So you could look at the contents of libxml2.a with ar t libxml2.a
Let's say for simplicity that it only contained: xml2a.o and xml2b.o
You want to combine those with your mine.o to make mine.a

You'll need to first do ar x libxml2.a to get out xml2a.o and xml2b.o
Then do ar cr mine.a mine.o xml2a.o xml2b.o

A simple explanation of the most widely used ar commands, though I've probably summarized what you'd get from it here.