0

What's the difference between:

ar -x liba.a
ar -x libb.a
ar rcs libab.a *.o

and

ar rcs libab.a liba.a libb.a

Are they really doing the same task?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Do you mean the difference between just the two "ar rcs" lines? And if so, did you mean "*.a" and not "*.o"? – Wayne Vosberg May 19 '17 at 15:41
  • Yes this two command do the same thing. No? What's the difference between extract with -x and create a static library with all .o or create library directly with .a? –  May 20 '17 at 21:07
  • No, they don't do exactly the same thing. Try "ar rcs libab1.a *.o" and "ar rcs libab2.a liba.a libb.a" and then compare the contents of each (ar tv .a). See also: http://stackoverflow.com/questions/3821916/how-to-merge-two-ar-static-libraries-into-one – Wayne Vosberg May 21 '17 at 10:46

1 Answers1

0

Unless you're using a version of ar that I don't know about, the effects of the two sequences of commands are quite different — though both end up creating a file libab.a.

The first sequence extracts all the (object) files from liba.a into the current directory, then all the files from libb.a (any name collisions mean the file from libb.a will survive), and then all the object files that are now in the directory (possibly including ones that were in neither liba.a nor libb.a) are archived in libab.a.

The second sequence creates an archive that itself contains two files: liba.a and libb.a. This is legal and feasible, but the resulting file isn't useful in linking programs. The linker doesn't look at nested archives; it will simply find no object files that supply any symbols (since neither liba.a nor libb.a is an object file), so the library will effectively be unused — though you'll not get an error from attempting to use it.

It is more likely that the first sequence of commands is useful than the second.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278