LinuxCommandLibrary

musl-gcc

Compile C/C++ code for musl libc

TLDR

View documentation for gcc

$ tldr gcc
copy

SYNOPSIS

musl-gcc [options] file...

PARAMETERS

-o file
    Place the output into file. This is the standard GCC option for specifying the output executable name.

-c
    Compile or assemble the source files, but do not link. The output will be object files.

-S
    Compile only; do not assemble or link. The output will be assembly files.

-E
    Preprocess only; do not compile, assemble or link. The output will be preprocessed source code.

-I dir
    Add the directory dir to the list of directories to be searched for header files.

-L dir
    Add the directory dir to the list of directories to be searched for libraries. musl-gcc often implicitly uses this to point to musl's library path.

-l library
    Link with the specified library. For example, -lm links with the math library.

-static
    Link against static libraries. While musl encourages static linking, this option explicitly forces it for all available libraries.

-g
    Produce debugging information in the operating system's native format (e.g., DWARF).

-Olevel
    Optimize the generated code. level can be 0, 1, 2, 3, s, or fast. Higher levels generally result in faster but larger code.

DESCRIPTION

musl-gcc is a convenient wrapper script or alias for the GNU Compiler Collection (GCC), specifically configured to link programs against the
musl C standard library instead of the more common GNU C Library (glibc).

The primary purpose of musl-gcc is to facilitate the creation of highly portable and lightweight executables. Musl is known for its small footprint, simplicity, and emphasis on static linking, which results in standalone binaries with minimal external dependencies. This makes musl-gcc particularly valuable for building applications for embedded systems, creating minimal Docker images, or cross-compiling for targets where glibc might be too large or complex.

Essentially, musl-gcc automates the process of passing the correct compiler and linker flags to GCC to ensure that the musl library is used for compilation and linking, streamlining the development of musl-based applications.

CAVEATS

musl-gcc is typically a component of a specific musl toolchain or a wrapper script found in environments like Alpine Linux. It is not a standalone command universally present on all Linux distributions by default.

Users may need to install a musl-based toolchain or compile GCC specifically for musl to use it.

While musl offers many advantages, some glibc-specific features (e.g., complex Name Service Switch (NSS) configurations, dynamic loading of shared libraries through dlopen in specific contexts) might behave differently or be unavailable.

STATIC LINKING EMPHASIS

One of the core philosophies behind musl is to facilitate and encourage static linking. When you compile with musl-gcc, the resulting executables are often statically linked by default or with minimal effort (e.g., using the -static flag). This means all necessary library code is bundled directly into the executable, eliminating external library dependencies and greatly improving portability across different Linux systems.

TOOLCHAIN INSTALLATION

To use musl-gcc, you typically need a musl-cross-toolchain. This is a GCC compiler suite built specifically to target the musl libc. It can be built from source (e.g., using projects like musl-cross-make) or installed via package managers on distributions that support it (e.g., alpine-sdk on Alpine Linux, or specific cross-compilation packages on other distros).

HISTORY

The musl C library project, initiated by Rich Felker, aimed to create a lightweight, efficient, and standards-compliant C library. As musl gained traction, particularly for embedded systems and minimalist Linux distributions like Alpine Linux, a clear need emerged for a convenient way to compile programs against it using the ubiquitous GCC compiler.

musl-gcc emerged as a practical solution, often implemented as a simple shell script or a specially configured GCC binary within a musl-based toolchain. Its development paralleled the growth of musl's adoption, providing a streamlined path for developers to leverage musl's benefits without manually managing complex GCC linker flags.

SEE ALSO

gcc(1), ld(1), ar(1), readelf(1), objdump(1)

Copied to clipboard