macos 如何确定 Mac OS X 上静态库 (.a) 的目标架构?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1085137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 06:15:33  来源:igfitidea点击:

How do I determine the target architecture of static library (.a) on Mac OS X?

iphonemacoslinkerbsduniversal-binary

提问by Justicle

I'm interested in verifying if a given iPhone static library has been built for ARM or Intel.

我有兴趣验证给定的 iPhone 静态库是否是为 ARM 或 Intel 构建的。

It's more curiosity than anything. Is there some kind of Mac OS X or BSD specific tool to do this? This postgives an example in Linux.

好奇心比什么都强。是否有某种 Mac OS X 或 BSD 特定工具可以执行此操作?这篇文章给出了一个 Linux 中的例子。

回答by Václav Slavík

Another option is lipo; its output is brief and more readable than otool's.

另一种选择是lipo; 它的输出比otool的更简短且更具可读性。

An example:

一个例子:

% lipo -info /usr/lib/libiodbc.a 
Architectures in the fat file: /usr/lib/libiodbc.a are: x86_64 i386 ppc
% lipo -info libnonfatarchive.a
input file libnonfatarchive.a is not a fat file
Non-fat file: libnonfatarchive.a is architecture: i386
%

回答by Logan Capaldo

filewill probably tell you. otoolcertainly should be able to. But I'd try filefirst, e.g.

file大概会告诉你。otool当然应该可以。但我会file先尝试,例如

logan:/Users/logan% file d2
d2: Mach-O executable ppc

Example with archive:

存档示例:

logan:/Users/logan% file /usr/lib/libMallocDebug.a
/usr/lib/libMallocDebug.a: Mach-O universal binary with 2 architectures
/usr/lib/libMallocDebug.a (for architecture i386):      current ar archive random library
/usr/lib/libMallocDebug.a (for architecture ppc):       current ar archive

回答by Jiahao Chen

As mentioned earlier, filedoes not always work. otool -hv -arch allis probably the closest thing that is guaranteed to work - it gives architecture information for every single object file in the library.

如前所述,file并不总是有效。otool -hv -arch all可能是保证工作的最接近的东西 - 它为库中的每个单个对象文件提供体系结构信息。

Example:

例子:

% otool -hv /sw/lib/libfftw3.a
Archive : /sw/lib/libfftw3.a
/sw/lib/libfftw3.a(align.o):
Mach header
      magic cputype cpusubtype  caps    filetype ncmds sizeofcmds      flags
MH_MAGIC_64  X86_64        ALL  0x00      OBJECT     3        336 SUBSECTIONS_VIA_SYMBOLS
/sw/lib/libfftw3.a(alloc.o):
Mach header
      magic cputype cpusubtype  caps    filetype ncmds sizeofcmds      flags
MH_MAGIC_64  X86_64        ALL  0x00      OBJECT     3        416 SUBSECTIONS_VIA_SYMBOLS
...

回答by Brian Vandenberg

As an alternative, I've found objdumpcan work well. As an example, in my environment I build library archives with vxWorks and need to link those into other projects. To test whether the archive is the correct architecture, I could do something like the following (bash syntax):

作为替代方案,我发现objdump可以很好地工作。例如,在我的环境中,我使用 vxWorks 构建库档案,并需要将它们链接到其他项目中。要测试存档是否是正确的架构,我可以执行以下操作(bash 语法):

if [ "$(objdumpsparc -a ${ARCHIVE_FILE} 2>&1 | ggrep -cvP 'elf32-sparc-vxworks')" -ne "0" ]; then
  echo "Cannot build with ${ARCHIVE_FILE}, it contains one or more non-sparc components"
fi;

This example isn't precisely correct, because some lines DO show up that don't say elf32-sparc-vxworks, but it's easy enough to adapt this.

这个例子并不完全正确,因为确实出现了一些没有说 elf32-sparc-vxworks 的行,但是很容易适应这一点。

One nice benefit of this is that objdump, or a similarly named variant, is installed on most *nix operating systems, whereas tools suggested in other responses aren't.

这样做的一个好处是,objdump大多数 *nix 操作系统上都安装了 或类似名称的变体,而其他响应中建议的工具则没有。

editIt just occurred to me the OP was asking on OSX. My apologies.

编辑我刚刚想到 OP 在 OSX 上询问。我很抱歉。

回答by bleater

This bash script will help you programmatically get a list of architectures into a variable.

这个 bash 脚本将帮助您以编程方式将架构列表放入一个变量中。

list_archs.sh:

list_archs.sh:

#! /bin/bash
lipo -info  | sed -En -e 's/^(Non-|Architectures in the )fat file: .+( is architecture| are): (.*)$//p'

Usage example:

用法示例:

./list_archs.sh /usr/lib/libc.dylib
x86_64 i386