为归档而构建的静态库,而不是链接的体系结构(x86_64)

时间:2014-02-28 23:16:02

标签: c++ macos static-libraries ar

当我尝试编译两个不同的程序时,我遇到了似乎是同样的问题。他们每个人首先创建一个静态库,然后创建链接该库的主应用程序。我正在使用gcc 4.7.2在Mac OS Mavericks上工作。

计划1

这是我运行make时发生的事情:

首先,创建了库libfeat.a,但我收到了警告:

ar rc ../lib/libfeat.a imgfeatures.o utils.o sift.o kdtree.o minpq.o xform.o
ranlib  ../lib/libfeat.a
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: warning for library: ../lib/libfeat.a the table of contents is empty (no object file members in the library define global symbols)

然后,在编译应用程序时,它说它不能使用该库,因为它不是为同一架构(x86_64)构建的:

gcc -O3 -I../include `pkg-config --cflags opencv` `pkg-config --cflags gtk+-3.0` `pkg-config --cflags gsl` siftfeat.c -o ../bin/siftfeat -L../lib -lfeat `pkg-config --libs opencv` `pkg-config --libs gtk+-3.0` `pkg-config --libs gsl`
ld: warning: ignoring file ../lib/libfeat.a, file was built for archive which is not the architecture being linked (x86_64): ../lib/libfeat.a

如果我运行lipo,我就明白了:

$ lipo -info ../lib/libfeat.a 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lipo: archive with no architecture specification: ../lib/libfeat.a (can't determine architecture for it)

计划2

我遇到了同样的问题,不同的程序也是如此:首先创建一个库,然后再使用它。

这是创建lib时的输出:

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

这是申请表:

c++ -o bundler -O3 -Wall -fpermissive  -I../lib/imagelib -I../lib/sfm-driver -I../lib/matrix -I../lib/5point -I../lib/sba-1.5 -I../lib/ann_1.1_char/include  -L../lib -L../lib/ann_1.1_char/lib \
    -D__NO_UI__ -D__BUNDLER__ -D__BUNDLER_DISTR__ BaseApp.o BundlerApp.o keys.o Register.o Epipolar.o Bundle.o BundleFast.o MatchTracks.o Camera.o Geometry.o ImageData.o SifterUtil.o BaseGeometry.o BundlerGeometry.o BoundingBox.o BundleAdd.o ComputeTracks.o BruteForceSearch.o BundleIO.o ProcessBundle.o BundleTwo.o Decompose.o RelativePose.o Distortion.o TwoFrameModel.o LoadJPEG.o -limage -lsfmdrv -lsba.v1.5 -lmatrix -lz -llapack -lblas -lcblas -lminpack -lm -l5point -ljpeg -lANN_char -lgfortran
ld: warning: ld: warning: ignoring file ../lib/libsba.v1.5.a, file was built for archive which is not the architecture being linked (x86_64)

在这种情况下,lipo确实告诉我库的架构是x86_64:

$ lipo -info lib/libsba.v1.5.a 
input file lib/libsba.v1.5.a is not a fat file
Non-fat file: lib/libsba.v1.5.a is architecture: x86_64

发生了什么事?

5 个答案:

答案 0 :(得分:18)

可能的原因是使用GNU ar(1) / ranlib(1)而不是Xcode工具链提供的GNU which -a ar / which -a ranlib。运行$PATH$ 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 [...] ,查看ar中的内容。

例如:

/usr/local/bin

/usr/bin相同。如果你像我一样,$PATH /usr/local/bin/usr/bincd /usr/local/bin mv ar gar ln -s /usr/bin/ar ar mv ranlib granlib ln -s /usr/bin/ranlib ranlib 中的GNU工具和JSON中的Xcode工具,那么你可以解决它:

"product_id"

答案 1 :(得分:4)

感谢this question with the same problem,我可以调查此问题。我对静态库没有太多经验,但我会尝试解释这个问题。

出于某种原因,Mac OSX ar实用程序在静态库中创建“子目录”。例如,构建sba库,make从目标文件构建静态库的步骤是:

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

之后,如果我查看静态库的内容,我看到除了文件之外,还有一些奇怪的导演:

$ 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

如果我们尝试提取这些文件,我们会收到有关子目录的一些错误:

$ 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

现在,如果我们使用提取的目标文件再次创建lib,它将起作用:

$ 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

我完全不了解原因,但它对我有用。

答案 2 :(得分:4)

使用libtool -static -a可能更容易使用静态库。

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

答案 3 :(得分:1)

我遇到了错误的架构错误消息的问题。它说如下:

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

lipo给出:非胖文件:../ lib / blahblah.a是架构:x86_64

在makefile中它说如下:      ARCH_FLAG = -arch x86_64 -arch i386

我评论了i386部分,错误消失了。      ARCH_FLAG = -arch x86_64#-arch i386

所以,我认为你可能因为同样的原因而得到错误。也许你只需要设置架构类来匹配你的库。

顺便说一句,我的makefile是由swig生成的,我没有为编译器设置任何开关。

答案 4 :(得分:1)

如果您已安装binutils,请取消链接或将其卸载

brew unlink binutils