Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ranlib and static library

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?

like image 796
pythonic Avatar asked Jul 05 '12 14:07

pythonic


People also ask

What is the difference between static library and dynamic library?

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.

What is Ranlib used for?

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.

Should I use static or dynamic library?

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.

What are the advantages of a static library over 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.


1 Answers

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".

like image 94
dadinck Avatar answered Sep 26 '22 04:09

dadinck