Static library built for archive which is not the architecture being linked (x86_64)

时光总嘲笑我的痴心妄想 提交于 2019-12-03 23:50:22

A possible cause is using the GNU ar(1)/ranlib(1) instead of the ones supplied by the Xcode toolchain. Run which -a ar and which -a ranlib to see the what you have in $PATH.

For example:

$ which -a ranlib
/usr/local/bin/ranlib
/usr/bin/ranlib

$ /usr/local/bin/ranlib --version
GNU ranlib (GNU Binutils) 2.28.51.20170105
Copyright (C) 2017 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or (at your option) any later version.
This program has absolutely no warranty.

$ /usr/bin/ralib --version
error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: unknown option character `-' in: --version
Usage: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib [-sactfqLT] [-] archive [...]

Same for ar. If you're like me and had /usr/local/bin preceeding /usr/bin in $PATH, with the GNU tools in /usr/local/bin and the Xcode ones in /usr/bin, you can fix it with:

cd /usr/local/bin
mv ar gar
ln -s /usr/bin/ar ar
mv ranlib granlib
ln -s /usr/bin/ranlib ranlib
ChronoTrigger

Thanks to this question with the same problem, I could look into this problem. I don't have much experience with static libraries, but I'll try to explain the problem.

For some reason, Mac OSX ar utility creates "subdirectories" in the static library. For example, building the sba library, the steps of make to build the static library from the object file is:

ar crv libsba.v1.5.a sba_levmar.o sba_levmar_wrap.o sba_lapack.o sba_crsm.o sba_chkjac.o

After that, if I look at the contents of the static library, I saw that in addition to the files, there are some strange directorios:

$ ar -t libsba.v1.5.a 
__.SYMDEF
/
//
sba_levmar.o/
/0
sba_lapack.o/
sba_crsm.o/
sba_chkjac.o/
sba_levmar.o
sba_levmar_wrap.o
sba_lapack.o
sba_crsm.o
sba_chkjac.o
sba_levmar.o
sba_levmar_wrap.o
sba_lapack.o
sba_crsm.o
sba_chkjac.o

If we try to extract those files, we get some errors regarding the subdirectories:

$ ar -x libsba.v1.5.a 
ar: /: Is a directory
ar: //: Is a directory
ar: sba_levmar.o/: Not a directory
ar: /0: Permission denied
ar: sba_lapack.o/: Not a directory
ar: sba_crsm.o/: Not a directory
ar: sba_chkjac.o/: Not a directory

Now, if we create the lib again with the extracted object files, it will work:

$ ar crv libsba.v1.5.a lib_o/*.o
a - lib_o/sba_chkjac.o
a - lib_o/sba_crsm.o
a - lib_o/sba_lapack.o
a - lib_o/sba_levmar.o
a - lib_o/sba_levmar_wrap.o

$ ar -t libsba.v1.5.a
__.SYMDEF SORTED
sba_chkjac.o
sba_crsm.o
sba_lapack.o
sba_levmar.o
sba_levmar_wrap.o

I don't understand the reason at all, but it worked for me.

prosseek

Using libtool -static -a might be simpler to get working static library.

Static library link issue with Mac OS X: symbol(s) not found for architecture x86_64

If you have binutils installed just unlink or uninstall it

brew unlink binutils

I had the problem of getting the wrong architecture error message. It said the following:

 ld: warning: ignoring file blah/lib/blahblah.a, file was built for archive which is not the architecture being linked (i386)

lipo gives: Non-fat file: ../lib/blahblah.a is architecture: x86_64

in the makefile it said the following: ARCH_FLAG = -arch x86_64 -arch i386

I commented out the i386 part and the error disappeared. ARCH_FLAG = -arch x86_64 #-arch i386

So, I think it is possible you are getting the error for the same reason. Maybe you just need to set the architecture class to match your library.

By the way, my makefile was generated by swig, and I had not set any switches for the compiler.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!