30

I am trying to implement backtrace functionality for a large framework, which is used for different platforms and OS'es. In some of them, it is linked against glibc, while in the other, something different (eg. uclibc) is used. backtrace() function exists only in the former.

Is there any way to tell whether glibc is used? Any #define? I was unable to find an answer in glibc manual. I know I can't have linking-time information during compilation, but I guess include files have to differ. At least backtrace have to be declared somewhere. I would like to check it without being forced to pass explicit flags to the compiler.

5 Answers 5

39

Include features.h, it contains the macros you need, e.g.

#define __GNU_LIBRARY__ 6

/* Major and minor version number of the GNU C library package.  Use
   these macros to test for features in specific releases.  */
#define __GLIBC__       2
#define __GLIBC_MINOR__ 4
6
  • 4
    10 seconds is neither here nor there. You get my vote for including snippet from feature.h ;)
    – Shawn Chin
    Commented Nov 24, 2010 at 12:09
  • And from me +1 for being a fair loser ;-) Commented Nov 24, 2010 at 14:10
  • Thanks drhirsch, you've already got your +1 when I noticed that I was too late. Commented Nov 24, 2010 at 14:30
  • 2
    In 2.15 there's a nice macro: __GLIBC_PREREQ(maj, min) that can check the max and min for you. Not sure how long it goes back for though. Commented Feb 1, 2013 at 11:57
  • 2
    __GLIBC_PREREQ has existed since 1999-06-18 according to ChangeLog.10 -- I think the first release is then glibc 2.2.
    – Josh Stone
    Commented Sep 12, 2014 at 20:59
30

There are the #defines __GNU_LIBRARY__, __GLIBC__ and __GLIBC_MINOR__ (6, 2 and 11 on my system with glibc-2.11) in features.h.

0
6

Checking for preprocessor macros is not a good solution. uClibc and possibly other libc implementations define macros to mimic glibc (without providing all of its bloated functionality) for much the same reasons that all browsers include "Mozilla" in their User-Agent strings: broken programs that expect to see glibc and turn off lots of features if they don't see it.

Instead you should write a configure script to probe for backtrace and use it only if it's available.

4
  • 2
    Configure scripts are not a useful solution for a project that needs to build on Windows and iPhone as well as Linux targets. Commented Sep 17, 2014 at 21:03
  • @LoganPickup: I don't see why not. But if you insist on using a build system that's not amenable to configure on those targets, you can just have hard-coded configure results (config.h, etc.) for them since they're essentially single, tightly-controlled environments. Commented Sep 17, 2014 at 23:20
  • 1
    Use CMake for multi platform projects. What you want is system introspection, not configure scripts per se. CMake does multi platform system introspection
    – Joakim
    Commented Jan 14, 2016 at 15:19
  • 2
    @Joakim - CMake is probably one of the worse choices. If he uses CMake he will have to write his own test that configure would perform for him. We just removed CMake as an alternative because it was so anemic, created so much extra work and caused so many problems. For a detailed list of problems see CMake Removal.
    – jww
    Commented Sep 17, 2017 at 3:40
2

Empirically, both of the following compile and run fine on GCC 6.4:

#include <stdio.h>

int main(void) {
#ifdef __GLIBC__
    puts("__GLIBC__");
#endif
    return 0;
}

and:

int main(void) {
#ifdef __GLIBC__
    puts("__GLIBC__");
#endif
    return 0;
}

but only the first produces output of course.

This must mean that __GLIBC__ comes from stdio.h which must include features.h, see also: What is the purpose of features.h header?

Therefore, strictly speaking, __GLIBC__ by itself is not a clear indication that glibc is used, since even without headers, GCC already embeds runtime objects such as crt1.o in the finale executable, and those come from glibc.

So the main missing question is: does glibc guarantee that features.h gets included by every header? I could not find a clear documentation quote. TODO.

2
#if defined(__GLIBC__) && !defined(__UCLIBC__) && !defined(__MUSL__)

This is getting a bit ugly and syntactically ambiguous, but useful.

1
  • 3
    To future readers, there is no __MUSL__ define, see the musl faq for the reasoning behind this. You're left with probing for functionality at runtime instead.
    – troglobit
    Commented Feb 14, 2021 at 4:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.