【问题标题】:Check glibc version for a particular gcc compiler检查特定 gcc 编译器的 glibc 版本
【发布时间】:2025-12-07 05:50:01
【问题描述】:

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

【问题讨论】:

  • 如果您想在编译时执行检查,那么下面 Zwol 的回答可能是最好的方法。如果您想在运行时检查版本,那么 R1tschY 的答案可能是最好的方法。请注意,由于 Linux 无法自行获取路径,您可能无法在运行时获得您期望的 Glibc 版本或标准 C++ 库版本。另见Linking g++ 4.8 to libstdc++

标签: c gcc glibc


【解决方案1】:

编写一个测试程序(例如命名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版本就会显示出来。

【讨论】:

  • 它对我有用,但这是在哪里记录的?我正在寻找at the glibc 2.7 docs,但找不到。
  • 该函数是 Gnulib 的一部分:gnu.org/software/gnulib/manual/gnulib.html
  • 你不能只放ldd --version吗?
  • 我使用 OSX,我得到 ==> 致命错误:'gnu/libc-version.h' 文件未找到
  • OSX从未使用过 GNU C 库;它的 Unix 用户空间都是 BSD 派生的。它最初确实使用 GCC 作为其系统编译器,但 Apple 作为一个组织对 GPLv3 过敏;在 GCC 的许可变更之后,他们开始大量资助 LLVM 并非偶然。
【解决方案2】:

更简单

使用 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.

...

【讨论】:

  • 上述命令的结果不一样。在我的电脑上:GNU libc 版本:2.17, ld-linux-x86-64.so.2 (GLIBC_2.3) => /lib64/ld-linux-x86-64.so.2 ??
【解决方案3】:

使用-print-file-namegcc选项:

$ gcc -print-file-name=libc.so
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../lib64/libc.so

这给出了路径。现在:

$ file /usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../lib64/libc.so
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../lib64/libc.so: ASCII C program text

$ cat /usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../lib64/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 ( /lib64/libc.so.6 /usr/lib64/libc_nonshared.a  AS_NEEDED ( /lib64/ld-linux-x86-64.so.2 ) )

看起来像一个链接器脚本。 libc 在 Linux 上的特殊之处在于它可以被执行:

$ /lib64/libc.so.6
GNU C Library stable release version 2.13, by Roland McGrath et al.
Copyright (C) 2011 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 4.5.1 20100924 (Red Hat 4.5.1-4).
Compiled on a Linux 2.6.35 system on 2011-08-05.
Available extensions:
    Support for some architectures added on, not maintained in glibc core.
    The C stubs add-on version 2.1.2.
    crypt add-on version 2.1 by Michael Glad and others
    GNU Libidn by Simon Josefsson
    Native POSIX Threads Library by Ulrich Drepper et al
    BIND-8.2.3-T5B
    RT using linux kernel aio
libc ABIs: UNIQUE IFUNC
For bug reporting instructions, please see:
<http://www.gnu.org/software/libc/bugs.html>.

【讨论】:

  • 哎呀,我以为你需要路径,我的错。
【解决方案4】:

gnu_get_libc_version 标识 GNU C 库的 runtime 版本。

如果您关心的是 compile-time 版本(即在/usr/include 中提供标头的版本),您应该查看宏__GLIBC____GLIBC_MINOR__ .这些扩展为正整数,并将被定义为包含 GNU C 库提供的 any 头文件的副作用;这意味着您可以包含标准标头,然后使用#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 年的“libc6 过渡”)。

如果您希望使用 shell 'one-liner' 来转储这些宏,请使用:

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

grep 模式被选择为仅匹配两个相关的宏,因为有几十个名为 __GLIBC_somethingorother 的内部宏您不想通读。

【讨论】:

  • 来自 cmets 的一个相关问题:echo '#include &lt;errno.h&gt;' | gcc -x c -dM -E - | egrep -i '(gnu|lib)'
  • uclibc-ng 支持gnu_get_libc_version(),所以“Not the GNU C Library”并不准确。
  • @pevik 它是否定义了__GLIBC__? (不应该。)
  • @zwol 是的,确实如此:在features.h 中,在gnu/libc-version.h 中使用。
  • @pevik 我会说这是 uclibc 中的一个错误,除非它已经支持每一个 glibc 扩展,这似乎不太可能。
【解决方案5】:

我怀疑你的系统中是否安装了多个 glibc。但ldd -v &lt;path/to/gcc-4.x&gt; 应该会给你使用的 glibc。

【讨论】:

    【解决方案6】:

    最简单的方法是使用ldd 附带glibc

    只需运行此命令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

    2. 检查使用的 libc.so 文件的版本。这种方式有点难度。您可以在此链接中查看:Linux: Check the glibc version

    【讨论】:

      【解决方案7】:

      您可以使用字符串命令来检查编译器的 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
      

      【讨论】:

      • 请注意,这并不能真正告诉您版本。假设二进制文件链接到 glibc 2.17,但碰巧没有引用任何符号版本晚于GLIBC_2.14 的符号。然后它可以轻松生成相同的符号版本列表。
      【解决方案8】:

      另外,检查 libc 的更高版本符号:

      readelf -V /lib64/libc.so.6
      

      【讨论】:

        最近更新 更多