我正在尝试创建一个脚本来检测二进制文件并获取编译它的版本。例如,如果我有一个使用 gcc
编译的可执行文件,我想获取 gcc
的版本。我发现我可以使用 Linux 命令 ldd 来检查可执行文件,但是有没有正确/更干净和更好的方法来执行此操作?
据我了解,一些 python 文件也使用 gcc
进行编译。如何检测版本?
最佳答案
这取决于您正在查看的二进制格式。正如您提到的 Linux,我假设是 ELF 文件。
为此,您可以使用objdump -s --section .comment file
:
my_executable: file format elf64-x86-64
Contents of section .comment:
0000 4743433a 20285562 756e7475 20372e33 GCC: (Ubuntu 7.3
0010 2e302d31 36756275 6e747533 2920372e .0-16ubuntu3) 7.
0020 332e3000 3.0.
对于 clang 来说,这看起来像
my_executable: file format elf64-x86-64
Contents of section .comment:
0000 4743433a 20285562 756e7475 20382e31 GCC: (Ubuntu 8.1
0010 2e302d31 7562756e 74753129 20382e31 .0-1ubuntu1) 8.1
0020 2e300063 6c616e67 20766572 73696f6e .0.clang version
0030 20362e30 2e302d31 7562756e 74753220 6.0.0-1ubuntu2
0040 28746167 732f5245 4c454153 455f3630 (tags/RELEASE_60
0050 302f6669 6e616c29 00 0/final).
另一个选项,解析起来有点困难,是使用字符串
:
strings hyrise/build-clang/hyriseClient | grep clang
clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final) | head -n 1
请注意,在 Linux 上,clang 可能使用 gcc 中的 ld,因此即使可执行文件是使用 clang 构建的,您也可能会在输出中看到“GCC”和“clang”。
关于linux - 查找已编译的可执行文件的版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53741431/