【发布时间】:2019-01-24 10:03:21
【问题描述】:
当尝试构建一个使用 clang 链接静态库的二进制文件(参见下面的 MWE)时,我收到以下错误消息:
⟩⟩⟩ clang -o test bar.a test.o
ld: warning: ignoring file bar.a, file was built for archive which is not the architecture being linked (x86_64): bar.a
> Undefined symbols for architecture x86_64:
> "_bar", referenced from:
> _main in test.o
> "_foo", referenced from:
> _main in test.o
> ld: symbol(s) not found for architecture x86_64
但根据lipo,架构正确且一致 (x86_64):
⟩⟩⟩ lipo -info test.o bar.a
input file bar.a is not a fat file
Non-fat file: test.o is architecture: x86_64
Non-fat file: bar.a is architecture: x86_64
otools -hv 显示类似的输出。 所有目标文件都是为 x86_64 构建的。那么这个错误信息是什么意思呢?
这里有一个完整的、最小的、工作示例来重现上面显示的问题:
-
foo.c:int foo(void) { return 1; } -
bar.c:int bar(void) { return 2; } -
test.c:#include <stdio.h> int foo(void); int bar(void); int main(void) { printf("foo = %d\n", foo()); printf("bar = %d\n", bar()); }
编译:
clang -c -o foo.o foo.c
ar rcs foo.a foo.o
clang -c -o bar.o bar.c
ar rcs bar.a foo.a bar.o
clang -c -o test.o test.c
clang -o test bar.a test.o
【问题讨论】:
-
此错误消息还有许多其他问题,但这些都与由于交叉编译导致的实际架构不匹配有关。我问了这个问题是因为目前无法通过谷歌搜索与我遇到相同问题的人的错误消息。希望这对其他人有帮助。
-
而且,如果有人对轶事感兴趣:调试和修复这个问题在复杂的代码库中花了我几天时间。