Check glibc version for a particular gcc compiler

后端 未结 8 1208
既然无缘
既然无缘 2020-11-29 19:13

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

8条回答
  •  抹茶落季
    2020-11-29 19:31

    gnu_get_libc_version identifies the runtime version of the GNU C Library.

    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.

    Expanding the test program from the accepted answer:

    #include 
    #ifdef __GLIBC__
    #include 
    #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

    Not the GNU C Library
    

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

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

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

    echo '#include ' | 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.

提交回复
热议问题