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

Met*_*est 46 c gcc glibc

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

R1t*_*chY 40

编写测试程序(例如命名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);
}
Run Code Online (Sandbox Code Playgroud)

并使用gcc-4.4编译器编译它:

gcc-4.4 glibc-version.c -o glibc-version
Run Code Online (Sandbox Code Playgroud)

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

  • 你不能只放`ldd --version`吗? (3认同)
  • OSX _从不_使用 GNU C 库;它的 Unix 用户空间都是 BSD 派生的。它最初确实使用 GCC 作为其系统编译器,但 Apple 作为一个组织对 GPLv3 过敏;在 GCC 的许可变更后,他们开始大力资助 LLVM 并不是偶然的。 (2认同)

Max*_*kin 32

使用-print-file-name gcc选项:

$ gcc -print-file-name=libc.so
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../lib64/libc.so
Run Code Online (Sandbox Code Playgroud)

这给了路径.现在:

$ 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 ) )
Run Code Online (Sandbox Code Playgroud)

看起来像链接器脚本.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>.
Run Code Online (Sandbox Code Playgroud)


Joh*_*ohn 29

更容易

使用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
Run Code Online (Sandbox Code Playgroud)

...

这与运行我的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.
Run Code Online (Sandbox Code Playgroud)

...

  • 上述命令的结果是不一样的。在我的电脑上:GNU libc 版本:2.17, ld-linux-x86-64.so.2 (GLIBC_2.3) =&gt; /lib64/ld-linux-x86-64.so.2 ?? (2认同)

zwo*_*wol 15

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
}
Run Code Online (Sandbox Code Playgroud)

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

Not the GNU C Library
Run Code Online (Sandbox Code Playgroud)

但是当编译并在附近的Linux机器上运行它会打印出来

GNU libc compile-time version: 2.24
GNU libc runtime version:      2.24
Run Code Online (Sandbox Code Playgroud)

在正常情况下,"运行时"版本可能比"编译时"版本大,但从不小.主要版本号不太可能再次改变(最后一次改变是1997年的"libc6过渡").

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

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

grep选择模式匹配只有两个宏是相关的,因为有几十命名内部宏__GLIBC_somethingorother,你不希望有通读.


iti*_*avi 9

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


DIN*_*LIT 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.
Run Code Online (Sandbox Code Playgroud)

他们是找出glibc版本的另外两种方法:

  1. 通过运行以下命令检查已安装的glibc rpm软件包的版本:

    rpm -q glibc

  2. 检查使用的libc.so文件的版本。这样比较困难。您可以在此链接中检查它:Linux:检查glibc版本


Nee*_*raj 5

您可以使用 strings 命令来检查编译器的 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
Run Code Online (Sandbox Code Playgroud)

  • 请注意,这并不能真正告诉您版本。假设该二进制文件与 glibc 2.17 链接,但碰巧没有引用任何符号版本晚于“GLIBC_2.14”的符号。然后它可以轻松地生成相同的符号版本列表。 (4认同)