本文共 888 字,大约阅读时间需要 2 分钟。
最近写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> lslibtest.a main.cpp test.cpp test.h test.oxxxxxx:~/mkfile> xxxxxx:~/mkfile> xxxxxx:~/mkfile> file test.otest.o: GCC precompiled header (version 013) for C++xxxxxx:~/mkfile> xxxxxx:~/mkfile> nm test.onm: test.o: File format not recognizedxxxxxx:~/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 的问题, 还是第一次见, 算是长见识了。 记忆结论只能增长经验, 学会分析, 找出问题原因并解决, 才能提高能力。
转载地址:http://wiwti.baihongyu.com/