libtest.a: could not read symbols: Archive has no index; run ranlib to add one的原因和解决方案

        最近写makefile, 在main.cpp链接libtest.a的时候, 遇到了这个问题:libtest.a: could not read symbols: Archive has no index; run ranlib to add one, 百思不得其解, 怎么会有问题呢?  在网上找了下, 众说纷纭, 有的说g++版本不对, 有的说32位/64位不兼容, 各种说法都有。 而我的情况, 显然不是网上给出的情况。

        初步怀疑, 可能是libtest.a出了问题。 前面我们说过, 静态库实际上就是目标文件的集合, 于是乎从libtest.a中, 用ar命令解出test.o文件, 对test.o进行分析, 发现:

xxxxxx:~/mkfile> ar -x libtest.a 
xxxxxx:~/mkfile> ls
libtest.a  main.cpp  test.cpp  test.h  test.o
xxxxxx:~/mkfile> 
xxxxxx:~/mkfile> 
xxxxxx:~/mkfile> file test.o
test.o: GCC precompiled header (version 013) for C++
xxxxxx:~/mkfile> 
xxxxxx:~/mkfile> nm test.o
nm: test.o: File format not recognized
xxxxxx:~/mkfile>
        擦, test.o居然出了问题, 再看看test.o的生成过程, 果然就明白了。 原来我是这样生成的:

 g++ -c test.cpp test.h -o test.o
       呵呵哒, 多了test.h,  去掉就OK.  这个问题, 我们在之前的博文说过, 故不再赘述。


       libtest.a: could not read symbols: Archive has no index; run ranlib to add one 的问题, 还是第一次见, 算是长见识了。 记忆结论只能增长经验, 学会分析, 找出问题原因并解决, 才能提高能力。





  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值