在 LINUX 中确定 .a 库/存档是 32 位还是 64 位?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5665228/
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-08-04 00:42:48  来源:igfitidea点击:

In LINUX determine if a .a library/archive 32-bit or 64-bit?

linux32bit-64bitarchive

提问by cvsdave

We distribute in Linux a static lib in both 64-bit and 32-bit versions. When troubleshooting a customer, I would like my diagnostic shell script to quickly eliminate the issue by checking the .a archive file to detetmine whether it is 32 or 64 bit. The methods that occur to me are less than elegant:

我们在 Linux 中分发了 64 位和 32 位版本的静态库。在对客户进行故障排除时,我希望我的诊断 shell 脚本通过检查 .a 存档文件来确定它是 32 位还是 64 位,从而快速消除问题。我想到的方法并不优雅:

  1. extract a .o member and ask the "file" command (e.g., ELF 32-bit etc)

  2. start including a dummy member coded to indicate, e.g. 32bit.o/64bit.o and use "ar -t" to check

  1. 提取 .o 成员并询问“文件”命令(例如,ELF 32 位等)

  2. 开始包含一个编码来指示的虚拟成员,例如 32bit.o/64bit.o 并使用“ar -t”来检查

I have tried "strings xyz.a | grep 32" but this doesn't work well over versions. Not a heartbreaker problem, but if you know of an elegant solution, I would like to know.

我已经尝试过“strings xyz.a | grep 32”,但这在版本上效果不佳。不是一个令人心碎的问题,但如果你知道一个优雅的解决方案,我想知道。

回答by ColWhi

If there are functions that are specific to a particular version you could try nm then grep for the function.

如果有特定于特定版本的功能,您可以尝试使用 nm 然后 grep 该功能。

回答by Petesh

oops, that missing sed means that it was displaying to many items.

哎呀,缺少 sed 意味着它正在向许多项目显示。

Just in an answer:

只是在一个答案中:

count=$(nm foo.a | grep '^0' | head -1 | sed 's/ .*//' | wc -c)
((count == 17)) && echo 64bit
((count == 9)) && echo 32bit
((count == 0)) && echo '??bit'

How it's supposed to work:

它应该如何工作:

  • nm - get the symbols from the library
  • grep - get lines starting with a hex string (address of symbol in file)
  • head - get the first line
  • sed - remove everything past the whitespace, including the whitespace
  • wc - count the number of characters.
  • nm - 从库中获取符号
  • grep - 获取以十六进制字符串开头的行(文件中符号的地址)
  • head - 获取第一行
  • sed - 删除空格之后的所有内容,包括空格
  • wc - 计算字符数。

In a 32 bit environment, you get addresses made up of 8 hex digits, adding the new line gives you 9, In a 64bit environment, you get addresses made up of 16 hex digits, adding the new line gives you 17.

在 32 位环境中,您获得由 8 个十六进制数字组成的地址,添加新行给您9,在 64 位环境中,您获得由 16 个十六进制数字组成的地址,添加新行给您17.

回答by caf

objdumpseems like the best way:

objdump似乎是最好的方法:

objdump -f libfoo.a | grep ^architecture

回答by pankaj kapoor

The simplest way is to use the file command.

最简单的方法是使用 file 命令。

$file <.so file or .a file>

回答by Erik

Just use the file command; i.e. file library.so

只需使用 file 命令;IEfile library.so