【问题标题】:nm: File format not recognizednm:文件格式无法识别
【发布时间】:2018-04-10 16:38:50
【问题描述】:

我正在尝试使用 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 文件?例如,在我机器上的 .o 上运行文件给我ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
  • @Kevin 谢谢,您的评论是指向解决方案的正确指针。

标签: gcc cmake linker g++


【解决方案1】:

即使文件说这些是 ascii 文件

这就是你的问题:.o 文件应该是(编译的)object 文件,采用系统使用的任何本机格式,而 不是 ASCII 文件.

您很可能搞砸了您的Makefile(或CMakefile)。但是你没有给他们看,所以无法提供进一步的帮助。

附:将文件命名为test1.cpp.o 也是非常不寻常的——通常test1.cpp 被编译成test1.o

【讨论】:

  • 关于 P.S.:这不是“非常不寻常”;这是 CMake 为目标文件名生成的内容。它的冲突可能性较小(test1.cpptest1.c,编译时只要在文件名后面加上.o 就不会发生冲突)。
【解决方案2】:

我有选项 -MM 作为编译标志,这导致构建依赖列表而不是目标文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-15
    • 1970-01-01
    • 1970-01-01
    • 2021-01-13
    • 2013-06-12
    • 1970-01-01
    • 2011-05-25
    相关资源
    最近更新 更多