Mac OS X的静态库链接问题:找不到架构x86_64的符号

pro*_*eek 5 c macos clang static-libraries unix-ar

我正在尝试生成一个静态库并将其与执行二进制文件链接.

这是一个库函数:

#include <stdio.h>

int hello() {
    return 10;
}
Run Code Online (Sandbox Code Playgroud)

使用这些命令,我​​可以得到一个静态库.

gcc -c io.c 
ar -crv libio.a io.o
Run Code Online (Sandbox Code Playgroud)

有了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
Run Code Online (Sandbox Code Playgroud)

这是使用库的主要功能.

#include <stdio.h>
extern int hello();

int main(int argc, char *argv[]) {
    printf("%d", hello());
}
Run Code Online (Sandbox Code Playgroud)

但是,当我将对象与静态库链接时,我有错误.

gcc main.c -lio -o main -L.
Run Code Online (Sandbox Code Playgroud)

错误消息是:

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)
Run Code Online (Sandbox Code Playgroud)

我使用aras in /bin/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
Run Code Online (Sandbox Code Playgroud)

可能有什么问题?

pro*_*eek 8

该库应该已生成libtool -static.

gcc -c io.c 
libtool -static -o libio.a io.o
gcc main.c -lio -o main -L.
main
Run Code Online (Sandbox Code Playgroud)

返回

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
Run Code Online (Sandbox Code Playgroud)

来自此页面的提示.