本文介绍了静态库链接的问题与Mac OS X:符号(S)未找到x86_64的架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成静态库和执行的二进制链接。

I'm trying to generate a static library and link it with an execution binary.

这是一个库函数:

#include <stdio.h>

int hello() {
    return 10;
}

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

With these commands, I could get a static library.

gcc -c io.c
ar -crv libio.a io.o

使用唇-info ,我查了一下,是 x86_64的架构。

With lip -info, I checked it is x86_64 architecture.

ar> lipo -info libio.a
input file libio.a is not a fat file
Non-fat file: libio.a is architecture: x86_64

这是使用该库的主要功能。

This is the main function that uses the library.

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

int main(int argc, char *argv[]) {
    printf("%d", hello());
}

然而,当我与静态库链接对象,我有错误。

However, when I link the object with the static library, I have errors.

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)

我用 AR /斌/ AR ,和Mac OS X是10.10.2铿锵-602.0.53。

I use the ar as in /bin/ar, and Mac OS X is 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

什么可能是错误的?

What might be wrong?

推荐答案

图书馆应该有 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

从<一个提示href=\"https://books.google.com/books?id=W8JMajkrmokC&pg=PT343&lpg=PT343&dq=static+library+mac+os+x&source=bl&ots=QVaoR8IVlV&sig=PQeVClawL5CC2JajFIo0jyYCPxY&hl=en&sa=X&ei=jrGEVaqyK4W0yQToyoGoCg&ved=0CB0Q6AEwADgo#v=onepage&q=static%20library%20mac%20os%20x&f=false\"相对=nofollow>此页面。

这篇关于静态库链接的问题与Mac OS X:符号(S)未找到x86_64的架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-10 21:17