我正在尝试使用gcc工具链将库test.a链接到可执行文件。

使用cmake时我从未经历过类似的事情,直到一切似乎运行顺利为止

> [100%] Linking CXX executable test
ar qc lib/test.a  CMakeFiles/test.dir/keys.cpp.o CMakeFiles/test.dir/test2.cpp.o  CMakeFiles/test.dir/test3.cpp.o  CMakeFiles/test.dir/test4.cpp.o  CMakeFiles/test.dir/test5.cpp.o  CMakeFiles/test.dir/test6.cpp.o

链接器抱怨的地方
> test.a: error adding symbols: Archive has no index; run ranlib to add one
collect2: error: ld returned 1 exit status

test.a包含以下目标文件:
> ar -t test.a
test1.cpp.o
test2.cpp.o
test3.cpp.o
test4.cpp.o
test5.cpp.o
test6.cpp.o

nm无法识别文件格式:
nm -s test.a
nm: test1.cpp.o: File format not recognized
nm: test2.cpp.o: File format not recognized
nm: test3.cpp.o: File format not recognized
nm: test4.cpp.o: File format not recognized
nm: test5.cpp.o: File format not recognized
nm: test6.cpp.o: File format not recognized

即使文件说这些是ascii文件:
file test1.cpp.o
test1.cpp.o: ASCII text

我被困住了。

我如何找出问题所在?

最佳答案



那就是您的问题所在:.o文件应该是(编译的)目标文件,而您的系统使用的是任何本机格式,而不是ASCII文件。

您很可能搞砸了Makefile(或CMakefile)。但是您没有显示它们,因此无法提供进一步的帮助。

附言给文件test1.cpp.o命名也很不常见-通常test1.cpp被编译成test1.o

关于c++ - nm:无法识别文件格式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49758835/

10-11 22:40
查看更多