ar: POSIX way of creating static library containing similarly named objects

nick clifton nickc@redhat.com
Fri Apr 19 08:04:00 GMT 2013


Hi Miguel,

> Consider the following two objects,
>
>    src/namespace_foo/state.o
>    src/namespace_bar/state.o
>
> Can `ar' be used to create a static library comprised of the objects
> above without either of which replacing any symbols? In other words, both
> namespace_foo/state.o and namespace_bar/state.o are put into the same
> library but no symbols are replaced.

Use the 'q' option rather than the 'r' option when adding the object 
files to the library - and create the symbol index after adding the 
objects, rather than at the same time.  Ie:

   % ar cr libstate.a src/namespace_foo/state.o
   % ar q  libstate.a src/namespace_bar/state.o
   % ar s  libstate.a
   % readelf -c libstate.a
   Index of archive libstate.a: (2 entries, 0x8 bytes in the symbol table)
   Binary libstate.a(state.o) contains:
         foo
   Binary libstate.a(state.o) contains:
         foo

Note - you can also achieve the same effect by creating the library and 
its index with just a single command:

   % ar crs libstate.o src/namespace_foo/state.o src/namespace_bar/state.o
   % ar t libstate.o
   state.o
   state.o

This appears to be a bug in the implementation of AR - the "r" command 
line option should eliminate duplicate entries.

Also note - if you create the symbol index at the same time as you 
quick-append a duplicate file to the library, the duplicate will be 
discarded:

   % ar cr libstate.a src/namespace_foo/state.o
   % ar qvs libstate.a src/namespace_bar/state.o
   r - namespace_bar/state.o
   % ar t libstate.a
   state.o

This is another bug.  The "s" option is making the "q" option behave as 
if it were "r".

Cheers
   Nick




More information about the Binutils mailing list