What functions is the libm intended for?

Alexey picture Alexey · Jan 5, 2019 · Viewed 8.3k times · Source

As far as I know some math functions are contained in libc, while others are in libm. I've discovered that experimentally:

$ nm --dynamic --defined-only /lib/x86_64-linux-gnu/libm.so.6 | grep -w abs 
$ nm --dynamic --defined-only /lib/x86_64-linux-gnu/libc.so.6 | grep -w abs 
T abs

Is there a requirement concerning which mathematical functions must be provided by libm? Does libc and libm together provide all the math functions required by C standard?

Answer

Florian Weimer picture Florian Weimer · Jan 5, 2019

Language standards such as ISO C and ISO C++ do not concern themselves with matters such as linking.

POSIX only requires that the c99 compiler supports -lm, and that the functions declared in the headers <math.h>, <complex.h> and <fenv.h> are available for linking if -lm is specified. It is possible to meet this requirement if functions are defined in a library which is linked in by default.

With current glibc, the split of functions is mostly arbitrary, subject to a few limitations in the current implementation. (A long time ago, two threading libraries were supported, so all thread-related functionality had to go into libpthread, but this is no longer the case.) Other approaches are possible: musl puts everything into libc.a for static linking, and into the dynamic linker for dynamic linking.