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

时间:2021-10-25 01:52:48

I have two gcc compilers installed on my system, one is gcc 4.1.2 (default) and the other is gcc 4.4.4. How can I check the libc version used by gcc 4.4.4, because /lib/libc.so.6 shows the glibc used by gcc 4.1.2, since it is the default compiler.

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

6 个解决方案

#1


21  

Write a test program (name it for example glibc-version.c):

编写一个测试程序(例如: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);
}

and compile it with the gcc-4.4 compiler:

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

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

When you execute ./glibc-version the used glibc version is shown.

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

#2


26  

Use -print-file-name gcc option:

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

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

That gives the path. Now:

出的路径。现在:

$ 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 ) )

Looks like a linker script. libc is special on Linux in that it can be executed:

看起来像链接器脚本。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>.

#3


12  

even easier

更容易

use ldd --version

用ldd——版本

This should return the glibc version being used i.e.

这应该返回使用的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

...

which is the same result as running my libc library

与运行我的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.

...

#4


8  

I doubt if you have more than one glibc installed in your system.But ldd -v <path/to/gcc-4.x> should give you the glibc used.

我怀疑您的系统中是否安装了多个glibc。但ldd - v <路径 gcc 4。x> 应该给你使用glibc。

#5


7  

gnu_get_libc_version identifies the runtime version of the GNU C Library.

gnu_get_libc_version标识GNU C库的运行时版本。

If what you care about is the compile-time version (that is, the version that provided the headers in /usr/include), you should look at the macros __GLIBC__ and __GLIBC_MINOR__. These expand to positive integers, and will be defined as a side-effect of including any header file provided by the GNU C Library; this means you can include a standard header, and then use #ifdef __GLIBC__ to decide whether you can include a nonstandard header like gnu/libc-version.h.

如果您所关心的是编译时版本(即提供/usr/include中的头的版本),则应该查看宏__GLIBC__和__GLIBC_MINOR__。这些扩展到正整数,将被定义为包括GNU C库提供的任何头文件的副作用;这意味着您可以包含一个标准的标头,然后使用#ifdef __GLIBC__来决定是否可以包括一个非标准的头,比如gnu/libc-version.h。

Expanding the test program from the accepted answer:

从已接受的答案扩展测试程序:

#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
}

When I compile and run this program on the computer I'm typing this answer on (which is a Mac) it prints

当我在计算机上编译和运行这个程序时,我正在键入这个答案(这是一个Mac)。

Not the GNU C Library

but when compiled and run on a nearby Linux box it prints

但是当编译并运行在附近的Linux盒子上时,它会打印出来。

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

Under normal circumstances, the "runtime" version could be bigger than the "compile-time" version, but never smaller. The major version number is unlikely ever to change again (the last time it changed was the "libc6 transition" in 1997).

在正常情况下,“运行时”版本可能比“编译时”版本更大,但不会更小。主要版本号不太可能再次更改(上一次更改是在1997年的“libc6转换”)。

If you would prefer a shell 'one-liner' to dump these macros, use:

如果您想要一个shell 'one-liner'来转储这些宏,请使用:

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

The grep pattern is chosen to match only the two macros that are relevant, because there are dozens of internal macros named __GLIBC_somethingorother that you don't want to have to read through.

选择grep模式只匹配相关的两个宏,因为有几十个内部宏被命名为__GLIBC_somethingorother,您不想要阅读它们。

#6


-1  

You can use strings command to check GLIBC version of compiler. Highest version is applicable.

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

#1


21  

Write a test program (name it for example glibc-version.c):

编写一个测试程序(例如: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);
}

and compile it with the gcc-4.4 compiler:

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

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

When you execute ./glibc-version the used glibc version is shown.

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

#2


26  

Use -print-file-name gcc option:

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

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

That gives the path. Now:

出的路径。现在:

$ 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 ) )

Looks like a linker script. libc is special on Linux in that it can be executed:

看起来像链接器脚本。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>.

#3


12  

even easier

更容易

use ldd --version

用ldd——版本

This should return the glibc version being used i.e.

这应该返回使用的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

...

which is the same result as running my libc library

与运行我的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.

...

#4


8  

I doubt if you have more than one glibc installed in your system.But ldd -v <path/to/gcc-4.x> should give you the glibc used.

我怀疑您的系统中是否安装了多个glibc。但ldd - v <路径 gcc 4。x> 应该给你使用glibc。

#5


7  

gnu_get_libc_version identifies the runtime version of the GNU C Library.

gnu_get_libc_version标识GNU C库的运行时版本。

If what you care about is the compile-time version (that is, the version that provided the headers in /usr/include), you should look at the macros __GLIBC__ and __GLIBC_MINOR__. These expand to positive integers, and will be defined as a side-effect of including any header file provided by the GNU C Library; this means you can include a standard header, and then use #ifdef __GLIBC__ to decide whether you can include a nonstandard header like gnu/libc-version.h.

如果您所关心的是编译时版本(即提供/usr/include中的头的版本),则应该查看宏__GLIBC__和__GLIBC_MINOR__。这些扩展到正整数,将被定义为包括GNU C库提供的任何头文件的副作用;这意味着您可以包含一个标准的标头,然后使用#ifdef __GLIBC__来决定是否可以包括一个非标准的头,比如gnu/libc-version.h。

Expanding the test program from the accepted answer:

从已接受的答案扩展测试程序:

#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
}

When I compile and run this program on the computer I'm typing this answer on (which is a Mac) it prints

当我在计算机上编译和运行这个程序时,我正在键入这个答案(这是一个Mac)。

Not the GNU C Library

but when compiled and run on a nearby Linux box it prints

但是当编译并运行在附近的Linux盒子上时,它会打印出来。

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

Under normal circumstances, the "runtime" version could be bigger than the "compile-time" version, but never smaller. The major version number is unlikely ever to change again (the last time it changed was the "libc6 transition" in 1997).

在正常情况下,“运行时”版本可能比“编译时”版本更大,但不会更小。主要版本号不太可能再次更改(上一次更改是在1997年的“libc6转换”)。

If you would prefer a shell 'one-liner' to dump these macros, use:

如果您想要一个shell 'one-liner'来转储这些宏,请使用:

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

The grep pattern is chosen to match only the two macros that are relevant, because there are dozens of internal macros named __GLIBC_somethingorother that you don't want to have to read through.

选择grep模式只匹配相关的两个宏,因为有几十个内部宏被命名为__GLIBC_somethingorother,您不想要阅读它们。

#6


-1  

You can use strings command to check GLIBC version of compiler. Highest version is applicable.

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