How to tell if glibc is used

久未见 提交于 2019-12-01 02:07:42

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

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

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.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!