34

i tried making library with

ar -r -c -s libtestlib.a *.o

as given in this tutorial http://matrixprogramming.com/Tools/CompileLink.html

But on linking with library following error comes

g++ -o uni2asc uni2asc.o -L../Modules -ltestlib

../Modules/libtestlib.a: could not read symbols: Archive has no index; run ranlib to add one
collect2: ld returned 1 exit status

i tried with ranlib also but still the error comes.. im working with ubuntu9.10 Please suggest me some solution for this

Amarghosh
  • 58,710
  • 11
  • 92
  • 121
ima
  • 539
  • 2
  • 7
  • 15

4 Answers4

30

Your archive command looks fine, can you try the following. 1) Get the object files in the archive/static library

ar -t libtestlib.a

2) For each object file (say foo.o) from step 1

file foo.o 

This will tell you the format of the object file. If the object file was compiled for a different platform, this would cause a failure to build the index for the archive. To correct this you would need to recompile these files.
3) For each object file from step 1, do

nm foo.o

This will list the symbols exported from the file.

Jasmeet
  • 2,324
  • 16
  • 9
  • ar -t libtestlib.a accrpt.o charclass.o ci.o dist.o edorpt.o list.o sort.o stopword.o sync.o text.o unicode.o util.o wacrpt.o word.o – ima May 05 '10 at 03:38
  • 1
    That looks good as well. I suggest you rebuild the archive or try ar -s libtestlib.a to rebuild just the index. Also you can list the symbols from your object files and libraries via nm, by doing nm libtestlib.a or nm accrpt.o for example. Lets see if this helps to debug the problem. – Jasmeet May 05 '10 at 06:29
  • nm libtestlib.a nm: accrpt.o: File format not recognized nm: charclass.o: File format not recognized nm: ci.o: File format not recognized nm: dist.o: File format not recognized nm: edorpt.o: File format not recognized nm: list.o: File format not recognized nm: sort.o: File format not recognized nm: stopword.o: File format not recognized nm: sync.o: File format not recognized nm: text.o: File format not recognized nm: unicode.o: File format not recognized nm: util.o: File format not recognized nm: wacrpt.o: File format not recognized nm: word.o: File format not recognized – ima May 05 '10 at 07:40
  • Looks like your object files are not in the right format. Maybe they were compiled for a different platform ?. Try file stopword.o to see the format of the object files. Anyways you will need to recompile these, this of course requires having the source files. After that create the static library and link it with your application. – Jasmeet May 05 '10 at 08:15
  • Thank you Jasmeet. It worked. As you suggested,I recompiled those files and added to library. – ima May 05 '10 at 09:39
  • Good to hear that. I updated the answer to reflect our discussion here. – Jasmeet May 05 '10 at 18:27
  • Thanks, for me (building a qt project) I had two set of gcc toolchains in my windows path (one for android ndk and one for mingw). since the android ndk was listed first that resulted in inconsistent object files. – Alireza Kazemi Jun 06 '18 at 09:48
  • If you remove or change order of a conflicting toolchain from your windows path, then don't forget to reopen the cmd.exe otherwise the modified path is not used. – Alireza Kazemi Jun 06 '18 at 10:00
16

I was using MinGW to compile a windows app when I got the error, so I found the built-in MinGW commands:

i686-w64-mingw32-ar

And

x86_64-w64-mingw32-ar

Try using those instead of ar if you encounter the problem in MinGW. They both fixed the question's problem for me.

Cimbali
  • 11,012
  • 1
  • 39
  • 68
Macoy
  • 172
  • 1
  • 6
2

libtool also has an useful option: -export-symbols-regexp.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
0

I ran into the exact same problem when trying to compile the NBIS libraries. There is an option for

make install LIBNBIS=yes

which creates a single archive containing the other archive files. The gcc linker does not handle this gracefully and just emits the Archive has no index message. The fix is to leave the archives as separate files

make install LIBNBIS=no

Then just link the application to the required archive(s). The archive feed order is important to be sure that the linker identifies the required dependencies, then resolves them as it processes the .a files.

Mike
  • 1,276
  • 4
  • 16
  • 27