检查特定gcc编译器的glibc版本

tquggr8v  于 2022-11-13  发布在  其他
关注(0)|答案(8)|浏览(172)

我的系统上安装了两个gcc编译器,一个是gcc 4.1.2(默认),另一个是gcc 4.4.4。我如何检查gcc 4.4.4使用的libc版本,因为/lib/libc.so.6显示了gcc 4.1.2使用的glibc,因为它是默认编译器。

d5vmydt9

d5vmydt91#

此外,请检查libc得更高版本符号:

readelf -V /lib64/libc.so.6
xytpbqjk

xytpbqjk2#

更容易
使用ldd --version
这将返回正在使用的glibc版本,即

$ ldd --version

ldd (GNU libc) 2.17
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO

...
这与运行我的libc库的结果相同

$ /lib/libc.so.6 

GNU C Library (GNU libc) stable release version 2.17, by Roland McGrath et al.
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.

...

cs7cruho

cs7cruho3#

编写一个测试程序(例如命名为glibc-version.c):

#include <stdio.h>
#include <stdlib.h>
#include <gnu/libc-version.h>

int main(int argc, char *argv[]) {
  printf("GNU libc version: %s\n", gnu_get_libc_version());
  exit(EXIT_SUCCESS);
}

并使用gcc-4.4编译器对其进行编译:

gcc-4.4 glibc-version.c -o glibc-version

当您执行./glibc-version时,会显示使用的glibc版本。

tgabmvqs

tgabmvqs4#

使用-print-file-namegcc选项:

$ gcc -print-file-name=libc.so
/usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/libc.so

这给出了路径。让我们检查文件:

$ file $(gcc -print-file-name=libc.so)
/usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/libc.so: ASCII text

$ cat $(gcc -print-file-name=libc.so)
/* GNU ld script
   Use the shared library, but some functions are only in
   the static library, so try that secondarily.  */
OUTPUT_FORMAT(elf64-x86-64)
GROUP ( /lib/x86_64-linux-gnu/libc.so.6 /usr/lib/x86_64-linux-gnu/libc_nonshared.a  AS_NEEDED ( /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 ) )

该文件是一个链接器脚本,用于链接GROUP列表中的库。
在ELF平台上,/lib/x86_64-linux-gnu/libc.so.6是一个与位置无关的可执行文件,带有一个动态符号表(类似于共享库的符号表):

$ file /lib/x86_64-linux-gnu/libc.so.6
/lib/x86_64-linux-gnu/libc.so.6: symbolic link to libc-2.31.so

$ file $(readlink -f /lib/x86_64-linux-gnu/libc.so.6)
/usr/lib/x86_64-linux-gnu/libc-2.31.so: ELF 64-bit LSB shared object, x86-64, version 1 (GNU/Linux), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=1878e6b475720c7c51969e69ab2d276fae6d1dee, for GNU/Linux 3.2.0, stripped

$ /lib/x86_64-linux-gnu/libc.so.6
GNU C Library (Ubuntu GLIBC 2.31-0ubuntu9.9) stable release version 2.31.
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Compiled by GNU CC version 9.4.0.
libc ABIs: UNIQUE IFUNC ABSOLUTE
For bug reporting instructions, please see:
<https://bugs.launchpad.net/ubuntu/+source/glibc/+bugs>.
kxkpmulp

kxkpmulp5#

gnu_get_libc_version标识GNU C库的 * 运行时 * 版本。
如果你关心的是 * 编译时 * 版本(也就是在/usr/include中提供头文件的版本),你应该看看宏__GLIBC____GLIBC_MINOR__。它们扩展为正整数,并且将被定义为包含 * GNU C库提供的 * 任何 * 头文件的副作用;这意味着您可以包含一个标准的头文件,然后使用#ifdef __GLIBC__来决定是否可以包含一个非标准的头文件,如gnu/libc-version.h
从已接受的答案扩展测试程序:

#include <stdio.h>
#ifdef __GLIBC__
#include <gnu/libc-version.h>
#endif

int
main(void)
{
#ifdef __GLIBC__
  printf("GNU libc compile-time version: %u.%u\n", __GLIBC__, __GLIBC_MINOR__);
  printf("GNU libc runtime version:      %s\n", gnu_get_libc_version());
  return 0;
#else
  puts("Not the GNU C Library");
  return 1;
#endif
}

当我编译并运行这个程序时,我在电脑上输入这个答案(这是一个Mac),它打印

Not the GNU C Library

但当在附近的Linux机器上编译和运行时,它会打印

GNU libc compile-time version: 2.24
GNU libc runtime version:      2.24

在正常情况下,“运行时”版本可能比“编译时”版本大,但绝不会小。主版本号不太可能再改变(上一次改变是在1997年的“libc 6过渡”)。
如果您希望使用shell“一行程序”来转储这些宏,请用途:

echo '#include <errno.h>' | gcc -xc - -E -dM | 
    grep -E '^#define __GLIBC(|_MINOR)__ ' | sort

选择grep模式是为了只匹配两个相关的宏,因为有许多名为__GLIBC_somethingorother的内部宏,您不希望必须通读它们。

hgc7kmma

hgc7kmma6#

我怀疑你的系统中是否安装了不止一个glibc。但是ldd -v <path/to/gcc-4.x>应该会给予你使用的glibc。

u4dcyp6a

u4dcyp6a7#

最简单的方法是使用glibc附带的ldd
只需运行以下命令ldd --version

dina@dina-X450LA:~$ ldd --version
ldd (Ubuntu GLIBC 2.23-0ubuntu9) 2.23
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Written by Roland McGrath and Ulrich Drepper.

还有另外两种方法可以找到glibc版本:
1.检查已安装的glibc rpm软件包的版本:通过运行以下命令
rpm -q glibc
1.检查所用的www.example.com文件的版本libc.so。这种方法稍微困难一点。您可以在以下链接中检查:Linux: Check the glibc version

klh5stk1

klh5stk18#

您可以使用字符串命令来检查编译器的GLIBC版本。最高版本适用。

ubuntu1604:extra$ strings ./arm-unknown-linux-gnueabi/bin/arm-unknown-linux-gnueabi-gcc | grep GLIBC
    GLIBC_2.3
    GLIBC_2.8
    GLIBC_2.14
    GLIBC_2.4
    GLIBC_2.11
    GLIBC_2.2.5
    GLIBC_2.3.4

相关问题