1

For example, we have three following libraries:

1.1.......................lib_A.a

1.2.......................lib_B.a

1.3.......................lib_C.a

Now I want to create one library which consists of all the above static libraries.

I have used following command:

ar -crs lib_Z.a lib_A.a lib_B.a lib_C.a

When I create final executable with lib_Z.a, the compiler gives me the following error:

error adding symbols: archive has no index; run ranlib to add one

How to solve this issue?

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

1 Answers1

3

Your lib_Z.a contains no object files; it only contains other .a files, and the loader doesn't know how to handle that. Running ranlib wouldn't help; it wouldn't find any object files in the lib_Z.a archive. You have to extract the object files from the separate libraries and then build them all into the result:

mkdir Work
cd Work
ar -x ../lib_A.a
ar -x ../lib_B.a
ar -x ../lib_C.a
ar -crs ../lib_Z.a *.o
cd ..
rm -fr Work

The only trouble you can run into is if two of the libraries contain the same object file name (e.g. lib_A.a contains config.o and so does lib_B.a but they define different configurations). You would have to rename one (or both) of the object files:

…
ar -x ../lib_A.a
mv config.o A_config.o
ar -x ../lib_B.a
mv config.o B_config.o
…
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278