What functions is the libm intended for?
Asked Answered
C

1

13

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?

Casar answered 5/1, 2019 at 18:21 Comment(15)
libm contains the definitions of what is declared in math.h.Birdwatcher
"Does libc and libm together provide all the math functions required by C++ standard?" No, only what's required by C. If you need to know about some specific function, I recommend searching this excellent reference site.Palomino
@EtiennedeMartel math.h contains function abs, while libm doesn't.Casar
No, obviously libc and libm do not provide all the C++ math libraries. Lots of them are header only.Hoey
@MatthieuBrucher It was just mentioned that libm and libc must provide only C functionsCasar
en.wikipedia.org/wiki/C_mathematical_functions#libmMckeown
@LightnessRacesinOrbit Not all math.h functions go to libm, some of them go to libc.Casar
@Casar Tell that to Wikipedia; I'm just providing links, not making any arguments. Remember to cite your sources.Mckeown
@LightnessRacesinOrbit I've discovered this experimentally. nm --dynamic --defined-only /lib/x86_64-linux-gnu/libm.so.6 | grep -w abs return nothing, while nm --dynamic --defined-only /lib/x86_64-linux-gnu/libc.so.6 | grep -w abs returns T abs.Casar
libc and libm are both C libraries. The C++ lib is libstdc++. Better retag as a C question.Breslau
@Breslau Which standard (C or C++) states what must be provided by these libraries?Casar
No standard. The people that are behind the glibc decided to split the functions in two shared libraries. And abs() is in stdlib.h, not in math.h.Ventilator
Note that some platforms do not require you to link with -lm or its equivalent to get the mathematical functions from the standard C library — macOS is a specific example. Once upon a long time ago, having the library separate was sensible because the same basic hardware might or might not have floating point arithmetic available (Intel 80386 with optional 80387 FPU (floating point unit) for a late example — which also helps date when this was a problem). Nowadays, this is seldom an issue; it would be more sensible to make -lm optional. The rules in the embedded space are different.Semantic
Incidentally, back in the old days, sometimes there were different implementations of the printf() and scanf() functionality — with and without support for floating point (because the floating point support is quite big, which mattered when your program was limited to 64 KiB).Semantic
Ping @JonathanWakely. He may be able to answer some of the questions about GNU systems. He usually has unique insight.Ouidaouija
C
17

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.

Commonage answered 5/1, 2019 at 19:18 Comment(3)
macOS has all the maths functions in the main C library.Semantic
Is "glibc" a name for a group of libraries one of which is libm?Casar
glibc is the name of the C library on GNU/Linux systems. Its implementation is split across multiple dynamic shared objects. On most architectures, they are called libc.so.6, libm.so.6, libpthread.so.0, plus a few more.Commonage

© 2022 - 2024 — McMap. All rights reserved.