Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aggregate static libraries

I try to aggregate multiple .a files into a single one. The command is the following:

TARGET=libnumpy.a
DIR=build/lib.linux-x86_64-2.6/numpy
FILES=$(DIR)/core/_sort.a $(DIR)/core/multiarray.a $(DIR)/core/umath.a $(DIR)/core/scalarmath.a $(DIR)/core/umath_tests.a $(DIR)/core/multiarray_tests.a $(DIR)/lib/_compiled_base.a $(DIR)/numarray/_capi.a $(DIR)/fft/fftpack_lite.a $(DIR)/linalg/lapack_lite.a $(DIR)/random/mtrand.a

all:
    ar cr $(TARGET) $(FILES)

But the resulting library libnumpy.a is unusable. When trying to compile something with it, I get

libnumpy.a: could not read symbols: Archive has no index; run ranlib to add one

(I tried to run ranlib but it didn't solve the problem)

When doing nm libnumpy.a I get

nm: _sort.a: File format not recognized
nm: multiarray.a: File format not recognized
nm: umath.a: File format not recognized 
nm: scalarmath.a: File format not recognized
nm: umath_tests.a: File format not recognized
nm: multiarray_tests.a: File format not recognized
nm: _compiled_base.a: File format not recognized
nm: _capi.a: File format not recognized
nm: fftpack_lite.a: File format not recognized
nm: lapack_lite.a: File format not recognized
nm: mtrand.a: File format not recognized

What's wrong with this?

like image 424
sunmat Avatar asked May 03 '26 10:05

sunmat


2 Answers

You're adding archive files to an archive file. You shouldn't be doing that, you should be putting object files in an archive.

One way around that if you have GNU ar is to create thin archives (with the T switch):

GNU ar can optionally create a thin archive, which contains a symbol index and references to the original copies of the member files of the archives. Such an archive is useful for building libraries for use within a local build, where the relocatable objects are expected to remain available, and copying the contents of each object would only waste time and space. Thin archives are also flattened, so that adding one or more archives to a thin archive will add the elements of the nested archive individually. The paths to the elements of the archive are stored relative to the archive itself.

Otherwise, archive all the individual .o files that comprise your original .a files.

like image 63
Mat Avatar answered May 06 '26 01:05

Mat


As @Mat mentioned:

#ar x <archivefile>  / * Repeat for all archives */
#ar cv <all Obj fiels>
like image 24
Whoami Avatar answered May 06 '26 01:05

Whoami



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!