Unmanned Player
Unmanned Player

Reputation: 1199

How to detect libc name and version in cross-compiler environments?

I have a few cross-compilers on my linux desktop. Some use glibc and some use uclibc (and in future there could be other libcs too).

Currently, I can go into their sysroot directories and search for libc.so and try to find which file name it points to (for eg. libc.so -> libuClibc-1.0.12.so) and I fetch their names. But this won't last long if naming changes or if cross-compiler itself changes.

Is there a better/reliable programmatic way of detecting the name and version of the libc being used by a target cross compiler? Or are there any introspection tools available to get details about the target ceros

Note: There are a few similar questions here which normally point to #include <features.h> file or similar. That works fine if the generated code can also run on the host. But not suitable for cross-compile environments.

So has anyone been able to detect much more reliably?

EDIT: On the off chance someone comes here looking for detecting musl libc specifically, I've answered another question on SO that is related to this question.

Upvotes: 3

Views: 2771

Answers (1)

DaBler
DaBler

Reputation: 2851

To detect the libc name and version at compile time, you can check for preprocessor macros. The following script is just a proof of concept:

GCC_FEATURES=$(gcc -dM -E - <<< "#include <features.h>")

if grep -q __UCLIBC__ <<< "${GCC_FEATURES}"; then
    echo "uClibc"
    grep "#define __UCLIBC_MAJOR__" <<< "${GCC_FEATURES}"
    grep "#define __UCLIBC_MINOR__" <<< "${GCC_FEATURES}"
    grep "#define __UCLIBC_SUBLEVEL__" <<< "${GCC_FEATURES}"
elif grep -q __GLIBC__ <<< "${GCC_FEATURES}"; then
    echo "glibc"
    grep "#define __GLIBC__" <<< "${GCC_FEATURES}"
    grep "#define __GLIBC_MINOR__" <<< "${GCC_FEATURES}"
else
    echo "something else"
fi

The uClibc test must be done first, since uClibc defines macros to mimic glibc.

Another possibility could be calling ldd --version in your cross-compiler environment (the glibc version is in parentheses). But I'm not sure if this works with libraries other than glibc.

Upvotes: 5

Related Questions