c - Mac OS X : symbol(s) not found for architecture x86_64 的静态库链接问题

标签 c macos clang static-libraries unix-ar

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

这是一个库函数:

#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/

相关文章:

OS X 应用程序的 XCode 5 远程调试

macos - 如何从菜单项创建 'sheet' segue

c++ - 如何使用 ms code gen 添加/bigobj 切换到 clang?

c - 有没有适合替代C宏的推荐代码生成方式?

MySql 不能打开多个客户端

c - OpenGL:从读取文件的 while 循环中调用 glutPostRedisplay()

c - int (*arr)[20] 和 int arr[][7] 之间有什么区别

c - GCC 4.0 : How do you remove unnecessary -arch flags 的 R-Perl 安装问题

c++ - 链接器命令失败,sdl

c - 数组和字符的问题