I am trying to link a static library that I created, but I get this error.
libmine.a: could not read symbols: Archive has no index; run ranlib to add one
I tried to do ranlib libmine.a
but nothing changed, it still gives the same error. How can I solve this problem?
Static libraries, while reusable in multiple programs, are locked into a program at compile time. Dynamic, or shared libraries, on the other hand, exist as separate files outside of the executable file.
ranlib command in Linux is used to generate index to archive. ranlib generates an index to the contents of an archive and it will be stored in the archive. The index lists each symbol defined by a member of an archive which is simply relocatable object file.
You would use a DLL when you want to be able to change the functionality provided by the library without having to re-link the executable (just replace the DLL file, without having to replace the executable file). You would use a static library whenever you don't have a reason to use a dynamic library.
Another benefit of using static libraries is execution speed at run-time. Because the it's object code (binary) is already included in the executable file, multiple calls to functions can be handled much more quickly than a dynamic library's code, which needs to be called from files outside of the executable.
To see the symbols in an archive, use nm.
nm -s libmine.a
<output>
The entry points to the subroutines should be labled "T" as in
00000000 T _sub1 00000019 T _sub2
What switches did you use in "ar" to make the static library? I usually use "ar -r" as in
ar -r libmine.a mine.o yours.o
If you are still having problems, add the "-s" option
ar -s -r libmine.a mine.o yours.o
Also, be sure that there are no other "libmine.a" files in the path, or make an explicit path to your "libmine.a". It is possible the linker is picking up a different "libmine.a".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With