User:Sam/Musl porting notes

From Gentoo Wiki
Jump to:navigation Jump to:search


  • Plenty of macros defined in glibc are actually there for compatibility reasons, but then are depended on by upstreams. Musl drops these, so we have to use the actual POSIX-provided versions instead.

Common problems

MAXNAMLEN not defined here

  • MAXNAMLEN doesn't exist in musl. It's actually an alias in glibc.


  • Include <limits.h>.
  • If NAME_MAX is defined, use that (ifdef). If not, fall back to MAXNAMLEN.

MSG_TRYHARD undeclared

  • MSG_TRYHARD only exists in glibc (_GNU_SOURCE set).


  • Use MSG_DONTROUTE instead.

getopt was not declared in this scope

  • In musl, a lot of headers are moved into separate files.


  • Include <getopt.h>.

undefined reference to fts_read

  • glibc provides extra functions which are not part of POSIX.
  • Upstreams depend on these without doing so explicitly.


  • Use one of the various standalone packages. Here, we need sys-libs/fts-standalone.
  • Note that this is a common class of problems. See here for more information.
  • This is a simple fix, add this to e.g. DEPEND:
FILE /etc/portage/package.use
elibc_musl? ( sys-libs/fts-standalone )

error: assignment of read-only variable '[stdout|stdin|stderr]'

  • In musl stdout, stdin and stderr are read-only and cannot be set like in glibc [1]


Use freopen("standard-output-file", "w", stdout); instead of stdout = fopen ("standard-output-file", "w");


undefined reference to getpt

  • Same as above.


  • Replace getpt() with posix_openpt() (with appropriate flags).


  • "net-misc/vmnet-0.4: vmnet.c:(.text+<snip>): undefined reference to getpt" bug #712470

error.h: No such file or directory

  • musl does not include the error.h header because "these functions and variables are GNU extensions, and should not be used in programs intended to be portable."


  • Just remove the include for error.h, usually, and relevant code if possible & harmless (often the case).


  • "net-libs/iax-0.2.2-r3 : iax.c: fatal error: error.h: No such file or directory " bug #712510


Standalone packages

glibc includes various extra functions which are not part of POSIX, so musl does not include them. User:blueness has ported and added the common ones to Gentoo:

Porting tasks

If you discover a patch in another distro (or contribute one yourself!), please add PATCH to the keywords (if you have permissions) and comment with a link to the patch. File a new bug if one does not already exist.

Patches from other distros

If you're stuck, it may be worth seeing what other musl-using distros have done to fix the problem. Be aware that some distros, like Alpine, include compatibility packages by default (for now), so you may need to keep searching.

You can look at Alpine or Void Linux too, but they do not seem to have an easy listing of patches like the above.