---
title: "💪🏻 Detect Musl"
date: 2022-07-31T16:00:15+03:00
draft: false
tags: [linux, c, musl]
---

Пост — чистейшая копипаста с [SoF](https://stackoverflow.com/questions/58177815/how-to-actually-detect-musl-libc).

```sh
#! /bin/sh

tmpf=/tmp/musl.log

# Detect Musl C library.
libc=$(ldd /bin/ls | grep 'musl' | head -1 | cut -d ' ' -f1)
if [ -z $libc ]; then
    # This is not Musl.
    rm -f ${tmpf}
    exit 1
fi
$libc >${tmpf} 2>&1
vstr=$(cat ${tmpf} | grep "Version" | cut -d ' ' -f2)

v_major=$(echo $vstr | cut -d '.' -f1)
v_minor=$(echo $vstr | cut -d '.' -f2)
v_patch=$(echo $vstr | cut -d '.' -f3)

rm -f ${tmpf}

echo "-D__MUSL__ -D__MUSL_VER_MAJOR__=${v_major} -D__MUSL_VER_MINOR__=${v_minor} -D__MUSL_VER_PATCH__=${v_patch}"
```

```sh
$ ./detect-musl.sh 
  -D__MUSL__ -D__MUSL_VER_MAJOR__=1 -D__MUSL_VER_MINOR__=1 -D__MUSL_VER_PATCH__=24
```

```sh
$ /lib/ld-musl-x86_64.so.1
  musl libc (x86_64)
  Version 1.2.3
  Dynamic Program Loader
  Usage: /lib/ld-musl-x86_64.so.1 [options] [--] pathname [args]
```