Why do you have to link the math library in C?
The functions in stdlib.h
and stdio.h
have implementations in libc.so
(or libc.a
for static linking), which is linked into your executable by default (as if -lc
were specified). GCC can be instructed to avoid this automatic link with the -nostdlib
or -nodefaultlibs
options.
The math functions in math.h
have implementations in libm.so
(or libm.a
for static linking), and libm
is not linked in by default. There are historical reasons for this libm
/libc
split, none of them very convincing.
Interestingly, the C++ runtime libstdc++
requires libm
, so if you compile a C++ program with GCC (g++
), you will automatically get libm
linked in.
Remember that C is an old language and that FPUs are a relatively recent phenomenon. I first saw C on 8-bit processors where it was a lot of work to do even 32-bit integer arithmetic. Many of these implementations didn't even have a floating point math library available!
Even on the first 68000 machines (Mac, Atari ST, Amiga), floating point coprocessors were often expensive add-ons.
To do all that floating point math, you needed a pretty sizable library. And the math was going to be slow. So you rarely used floats. You tried to do everything with integers or scaled integers. When you had to include math.h, you gritted your teeth. Often, you'd write your own approximations and lookup tables to avoid it.
Trade-offs existed for a long time. Sometimes there were competing math packages called "fastmath" or such. What's the best solution for math? Really accurate but slow stuff? Inaccurate but fast? Big tables for trig functions? It wasn't until coprocessors were guaranteed to be in the computer that most implementations became obvious. I imagine that there's some programmer out there somewhere right now, working on an embedded chip, trying to decide whether to bring in the math library to handle some math problem.
That's why math wasn't standard. Many or maybe most programs didn't use a single float. If FPUs had always been around and floats and doubles were always cheap to operate on, no doubt there would have been a "stdmath".
Because of ridiculous historical practice that nobody is willing to fix. Consolidating all of the functions required by C and POSIX into a single library file would not only avoid this question getting asked over and over, but would also save a significant amount of time and memory when dynamic linking, since each .so
file linked requires the filesystem operations to locate and find it, and a few pages for its static variables, relocations, etc.
An implementation where all functions are in one library and the -lm
, -lpthread
, -lrt
, etc. options are all no-ops (or link to empty .a
files) is perfectly POSIX conformant and certainly preferable.
Note: I'm talking about POSIX because C itself does not specify anything about how the compiler is invoked. Thus you can just treat gcc -std=c99 -lm
as the implementation-specific way the compiler must be invoked for conformant behavior.
Because time()
and some other functions are builtin
defined in the C library (libc
) itself and GCC always links to libc unless you use the -ffreestanding
compile option. However math functions live in libm
which is not implicitly linked by gcc.
An explanation is given here:
So if your program is using math functions and including
math.h
, then you need to explicitly link the math library by passing the-lm
flag. The reason for this particular separation is that mathematicians are very picky about the way their math is being computed and they may want to use their own implementation of the math functions instead of the standard implementation. If the math functions were lumped intolibc.a
it wouldn't be possible to do that.
[Edit]
I'm not sure I agree with this, though. If you have a library which provides, say, sqrt()
, and you pass it before the standard library, a Unix linker will take your version, right?