Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

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.

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 5, 2010 at 3:38 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 5, 2010 at 6: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 5, 2010 at 7: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 5, 2010 at 8:15 Thank you Jasmeet. It worked. As you suggested,I recompiled those files and added to library. – ima May 5, 2010 at 9:39

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

You should be able to specify it as an environment variable at your shell's command line, e.g. AR=x86_64-w64-mingw32-ar ./configure. – Jason R May 4, 2015 at 20:59

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.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.