Why do you need an explicit `-lm` compiler option
Historical reasons
The reasons libc
and libm
are separated and you have to specify -lm
on the command line are historical reasons because the libm
was also used by the Fortran compiler.
It is to accomodate systems (mainly embedded) where floating point math is not possible or necessary. It's kind of historical indeed but don't forget that gcc
and most other C compilers were written in a time where a 386SX was considered a high performance processor.
To give an example, when I still worked in embedded computing, we used standard compilers (Microsoft and Borland) to generate code for our processors (Z80, 80186 and 68030). If the compilers had by default linked to the math library we would have been in trouble as none of our systems had floating point capabilities or even needed them.
It's true that 30 years afterwards it seems silly but the reason was sound at that time.
There are many libraries you may want, and libm
is just one of them.
For each of these, you may ask why it isn't included by default.
Perhaps libm
is more useful than others, but still, C prefers to keep things simple - you want a library, use -l
to use it.