【问题标题】:C++ 64 bits - could not read symbols: Archive has no index; run ranlib to add oneC++ 64 位 - 无法读取符号:存档没有索引;运行ranlib添加一个
【发布时间】:2011-07-30 10:30:31
【问题描述】:

我正在尝试使用静态库在 Linux RHAS 5.3 64 位上生成一个非常简单的二进制文件。

test1.cpp,生成的 .o 将嵌入到静态库中。

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

prog.cpp

#include <stdio.h>
void ctest1(int *);

int main()
{
   int x;
   ctest1(&x);
   printf("Valx=%d\n",x);

   return 0;
}

如果我用 32 位编译,没问题:

--(0931:Wed,06 Apr 11:$)-- g++ -m32 -Wall -c ctest1.cpp
--(0931:Wed,06 Apr 11:$)-- 文件 ctest1.o
ctest1.o:ELF 32 位 LSB 可重定位,英特尔 80386,版本 1 (SYSV),未剥离
--(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

但是,如果我尝试以 64 位编译,它会在链接过程中失败并显示错误“无法读取符号:存档没有索引;运行 ranlib 以添加一个”:

--(0933:Wed,06 Apr 11:$)-- g++ -m64 -Wall -c ctest1.cpp
--(0935:Wed,06 Apr 11:$)-- 文件 ctest1.o
ctest1.o:ELF 64 位 LSB 可重定位,AMD x86-64,版本 1 (SYSV),未剥离
--(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:无法读取符号:存档没有索引;运行ranlib添加一个
collect2: ld 返回 1 个退出状态

在 libctest.a 上运行 ranlib 不会改变任何内容。

我的Linux版本如下

--(0937:Wed,06 Apr 11:$)-- uname -a
Linux dev1 2.6.18-128.el5 #1 SMP 2008 年 12 月 17 日星期三 11:41:38 EST x86_64 x86_64 x86_64 GNU/Linux

有人知道问题出在哪里吗?

谢谢。

【问题讨论】:

    标签: c++ linker 64-bit


    【解决方案1】:

    您是否在使用 64 位版本重新编译之前删除了库?

    你的编译顺序对我有用:

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

    当我编译 32 位时:

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

    这是一些 RHEL 5 版本(不是所有最新版本):

    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
    

    我的 GCC 是 4.1.2 版。我的AR版本如下,RANLIB打印的版本一样:

    GNU ar 2.17.50.0.6-9.el5 20061020
    

    我不需要直接使用ranlib。

    【讨论】:

    • yes *.o, *.a et bin 文件在两代之间被删除。
    • BTW GCC 版本也是 4.1.2
    • 好的,我发现问题了,我机器上安装的binutils版本太旧了! GNU ar 2.10.90。我更新了它,现在我可以正确链接。感谢您的帮助!
    • @paf:很高兴能得到一些帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-01
    • 2012-02-11
    • 2021-02-22
    • 2011-01-17
    • 1970-01-01
    相关资源
    最近更新 更多