I'm confused why you have to type -lm to properly link math to your code, but don't have to do the same for stdio. I've only just started using C, so I apologize if this is a stupid quesiton or I'm missing something obvious.
Asked
Active
Viewed 2,380 times
5
-
3http://stackoverflow.com/questions/1033898/why-do-you-have-to-link-the-math-library-in-c?rq=1 – verbose Aug 05 '13 at 06:23
-
`*.h` files are header files, header files are **not** linked, headers are **`include`ed**! – alk Aug 05 '13 at 06:25
2 Answers
7
In short, because of historical reasons,
The functions in stdio.h
are in libc
, while the functions in math.h
are in libm
. libc
is linked by default but libm
isn't.

Yu Hao
- 119,891
- 44
- 235
- 294
-
-
And the historical reasons were, while virtually everybody needed the code in `libc`, the code in `libm` was required by only a few applications. – DevSolar Aug 05 '13 at 06:29
3
There are two different things:
- header files (
stdio.h
andmath.h
) - they contain only function prototypes and some definitions and data; they are#include
d in your source code - libraries (
libm.so
) - they contain binary code which will be linked back into your application (binary code). Also, for a library namedlibname.so
the linker flag is-lname
- forlibm.so
the flag is-lm
.
Take also in consideration that there are libc.so
and libstdc.so
which are always linked into your application. Code for functions in stdio.h
and stdlib.h
and several others is found on those libraries - thus, it is always included.
PS: I'm assuming Linux/UNIX here, thus the names are very specific. On Windows things are similar but with other names (DLLs instead of .so
files, etc.)

Mihai Maruseac
- 20,967
- 7
- 57
- 109
-
The names `libm.so`, `libc.so`, etc., are very system-specific. But then so is the requirement to use `-lm` to link the math library. – Keith Thompson Aug 05 '13 at 06:25
-