我正在尝试生成静态库并将其与执行二进制文件链接。
这是一个库函数:
#include <stdio.h>
int hello() {
return 10;
}
通过这些命令,我可以获得一个静态库。
gcc -c io.c
ar -crv libio.a io.o
使用lip -info
,我检查它是x86_64
架构。
ar> lipo -info libio.a
input file libio.a is not a fat file
Non-fat file: libio.a is architecture: x86_64
这是使用库的主要功能。
#include <stdio.h>
extern int hello();
int main(int argc, char *argv[]) {
printf("%d", hello());
}
但是,当我将对象与静态库链接时,出现错误。
gcc main.c -lio -o main -L.
错误信息是:
ld: warning: ignoring file ./libio.a, file was built for archive which is not the architecture being linked (x86_64): ./libio.a
Undefined symbols for architecture x86_64:
"_hello", referenced from:
_main in main-2c41a0.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我在 /bin/ar
中使用 ar
,而 Mac OS X 是 10.10.2 with clang-602.0.53。
ar> clang -v
Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.3.0
Thread model: posix
可能出了什么问题?
最佳答案
该库应使用 libtool -static
生成。
gcc -c io.c
libtool -static -o libio.a io.o
gcc main.c -lio -o main -L.
main
返回
10
ar> lipo -info libio.a
input file libio.a is not a fat file
Non-fat file: libio.a is architecture: x86_64
ar> file libio.a
libio.a: current ar archive
ar> nm libio.a
io.o:
0000000000000000 T _hello
来自 this page 的提示.
关于c - Mac OS X : symbol(s) not found for architecture x86_64 的静态库链接问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30948807/