Manipulating static libraries with ar

I came across a situation where I had to replace an object file in a static library. First problem encountered was that it was a “fat” library – it had the same code compiled for multiple architectures. The architectures included can be displayed with this command:

lipo -archs fred.a

To get around this, it was necessary to create a new thin library with just the architecture in which I was interested – arm64 in this case. This can be done with lipo again:

lipo fred.a -thin arm64 -output fred64.a

fred64.a will just contain the version for the chosen architecture. In my case, I wanted to replace a large set of object files so I decided to just expand the static library into all of its component object files:

ar x fred64.a

Then I could easily replace the necessary object files. Finally, I recreated the thin library:

ar cr fred64new.a *.o

It’s easy to check what is in the new library using:

ar t fred64new.a

For even more detail, the nm command will display the exposed symbols in the library:

nm fred64new.a
%d bloggers like this: