2015-06-19 37 views
3

我试图生成一个静态库并将其与执行二进制文件链接起来。Mac OS X的静态库链接问题:找不到x86_64体系结构的符号

这是一个库函数:

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

我使用ar/bin/ar,和Mac OS X是10.10.2铿锵-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 

什么可能是错误的?

+0

什么是真正的错误是,苹果不道德把周围铛/ LLVM的包装并把它称为GCC,当大多数系统GCC是指GNU GCC不是苹果的铛/ LLVM。因为liblto_plugin.so是一个gnu扩展,所以苹果的编译器不知道在哪里可以找到它,因为它不在系统路径或框架中。我已经使用了ar rcs lib * .a * .o,你根本不需要使用libtool,只需链接到gnu gcc,使用macports或brew进行安装。 –

+0

这两个问题不是完全重复的,但非常相似:[在Mac上没有正确添加到档案中的对象文件](https://stackoverflow.com/questions/34595766/object-files-not-properly-added-to- Mac OS X上的静态链接时找不到符号)(https://stackoverflow.com/questions/44343859/symbol-not-found-when-static-linking-on- MacOSX的/ 44355323#44355323)。 –

回答

4

该库应该使用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

+0

不,它实际上并不是。 –

0

从黑客CMake生成make文件(CMakeFiles/test.dir/link.txt),在默认情况下使用的/usr/local/ar中的ar似乎不能正常工作。

这是link.txt的内容。

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ar qc libtest.a CMakeFiles/test.dir/test.c.o 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib libtest.a 

从脚本中,/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ar是我必须使用的。

[email protected] ar> ls -alF /usr/bin/ar 
-rwxr-xr-x 1 root wheel 18160 Oct 17 18:49 /usr/bin/ar* 
[email protected] ar> ls -alF /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ar 
-rwxr-xr-x 1 root wheel 33472 Oct 29 16:36 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ar* 

同样地,应使用ranlib的是/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib不是默认之一。

[email protected] ar> ls -alF `which ar` 
-rwxr-xr-x 1 root wheel 18160 Oct 17 18:49 /usr/bin/ar* 
[email protected] ar> ls -alF /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib 
lrwxr-xr-x 1 root wheel 7 Nov 10 21:10 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/[email protected] -> libtool 

除此之外-qc选项需要使用(从cmake的生成脚本)

-c  Whenever an archive is created, an informational message to that 
     effect is written to standard error. If the -c option is speci- 
     fied, ar creates the archive silently. 
-q  (Quickly) append the specified files to the archive. If the ar- 
     chive does not exist a new archive file is created. Much faster 
     than the -r option, when creating a large archive piece-by-piece, 
     as no checking is done to see if the files already exist in the 
     archive. 

这些是得到正确的库文件的命令:

clang -c hellolib.cpp -o hellolib.o 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ar -qc libhello.a hellolib.o 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib libhello.a 

的用法是:

clang usehello.cpp -lhello -L. 

nm和脂显示正确的库文件信息:

[email protected] ar> nm libhello.a 
libhello.a(hellolib.o): 
0000000000000000 T __Z3addii 
[email protected] ar> lipo -info libhello.a 
input file libhello.a is not a fat file 
Non-fat file: libhello.a is architecture: x86_64 
相关问题