C++ 64 bits - could not read symbols: Archive has no index; run ranlib to add one

12,121

Did you delete the library before recompiling with the 64-bit build?

Your compilation sequence worked for me:

$ g++ -m64 -Wall -c prog.cpp
$ g++ -m64 -Wall -c test1.cpp
$ ar -cvq libtest.a test1.o
a - test1.o
$ g++ -m64 -Wall -o prog1 prog.o libtest.a
$ file test1.o prog.o
test1.o: ELF 64-bit LSB relocatable, AMD x86-64, version 1 (SYSV), not stripped
prog.o: ELF 64-bit LSB relocatable, AMD x86-64, version 1 (SYSV), not stripped
$ ./prog1
Valx=5
$ 

When I then compiled 32-bit:

$ g++ -m32 -Wall -c prog.cpp
$ g++ -m32 -Wall -c test1.cpp
$ file test1.o prog.o
test1.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped
prog.o:  ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped
$ ar -cvq libtest.a test1.o
a - test1.o
$ g++ -m32 -Wall -o prog1 prog.o libtest.a
/usr/bin/ld: warning: i386:x86-64 architecture of input file `libtest.a(test1.o)' is incompatible with i386 output
$ file prog1
prog1: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.6.9, dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped
$ ./prog1
Memory fault 
$ 

This is some RHEL 5 release (not all that current):

Linux toru 2.6.18-128.el5 #1 SMP Wed Dec 17 11:41:38 EST 2008 x86_64 x86_64 x86_64 GNU/Linux

My GCC is version 4.1.2. My AR version is as follows, and RANLIB prints the same version:

GNU ar 2.17.50.0.6-9.el5 20061020

I didn't need to use ranlib directly.

Share:
12,121
paf
Author by

paf

Updated on June 04, 2022

Comments

  • paf
    paf almost 2 years

    I am trying to generate a very simple binary on Linux RHAS 5.3 64bits using a static library.

    test1.cpp, whom resulting .o will be embedded in a static library.

    void ctest1(int *i)
    {
       *i=5;
    }
    

    and prog.cpp

    #include <stdio.h>
    void ctest1(int *);
    
    int main()
    {
       int x;
       ctest1(&x);
       printf("Valx=%d\n",x);
    
       return 0;
    }
    

    If I compile in 32 bits, no problem:

    --(0931:Wed,06 Apr 11:$ )-- g++ -m32 -Wall -c ctest1.cpp
    --(0931:Wed,06 Apr 11:$ )-- file ctest1.o
    ctest1.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped
    --(0931:Wed,06 Apr 11:$ )-- ar -cvq libctest.a ctest1.o
    a - ctest1.o
    --(0931:Wed,06 Apr 11:$ )-- g++ -m32 -o prog prog.cpp libctest.a
    --(0931:Wed,06 Apr 11:$ )-- ./prog
    Valx=5

    However, if I try to compile in 64 bits, it fails during the link with the error "could not read symbols: Archive has no index; run ranlib to add one":

    --(0933:Wed,06 Apr 11:$ )-- g++ -m64 -Wall -c ctest1.cpp
    --(0935:Wed,06 Apr 11:$ )-- file ctest1.o
    ctest1.o: ELF 64-bit LSB relocatable, AMD x86-64, version 1 (SYSV), not stripped
    --(0933:Wed,06 Apr 11:$ )-- ar -cvq libctest.a ctest1.o
    a - ctest1.o
    --(0935:Wed,06 Apr 11:$ )-- g++ -m64 -o prog prog.cpp libctest.a
    libctest.a: could not read symbols: Archive has no index; run ranlib to add one
    collect2: ld returned 1 exit status

    Running ranlib on libctest.a does not change anything.

    My Linux version is the following

    --(0937:Wed,06 Apr 11:$ )-- uname -a
    Linux dev1 2.6.18-128.el5 #1 SMP Wed Dec 17 11:41:38 EST 2008 x86_64 x86_64 x86_64 GNU/Linux

    Does anyone have an idea where the problem is coming from?

    Thanks.

  • paf
    paf about 13 years
    yes *.o, *.a et the bin file were deleted between both generation.
  • paf
    paf about 13 years
    BTW GCC version is also 4.1.2
  • paf
    paf about 13 years
    Ok I found the issue, the version of binutils installed on my machine is very old! GNU ar 2.10.90. I updated it and now i links properly. Thanks for your help!
  • Jonathan Leffler
    Jonathan Leffler about 13 years
    @paf: glad to have been of some assistance!