I could not found any documentation on why gcc
requires -lm
for math.h
functions but no -l
is required for stdio
or stdlib
functions. Why some functions require the include and the -l
gcc
command option, and others don't require the -l
option? Any thoughts?

- 133,132
- 16
- 183
- 261

- 47
- 4
-
2I don't know if it's part of the specification, but it's the traditional divide because on old systems the math function would otherwise take up a lot of space when they were not commonly used. – Some programmer dude Feb 04 '19 at 13:41
-
The functions in the includes `stdlib.h`, `stdio.h`, etc. almost all live in the same library, libc. – Mad Physicist Feb 04 '19 at 13:47
-
It's a bug, and you should complain about it to your vendor. – Steve Summit Feb 04 '19 at 15:30
1 Answers
There are some libraries which gets linked by default.
One of those default libraries for gcc
is libc.a
(static) or libc.so
(dynamic) (GNU standard C library), and it contains the definition of printf()
and scanf()
families, including others, prototyped in stdio.h
or stdlib.h
.
Now, to answer your question, according to the wikipedia article
Under FreeBSD and Linux,[8] the mathematical functions (as declared in
math.h
) are bundled separately in the mathematical librarylibm
. If any of them are used, the linker must be given the directive-lm
.
If you want to check explicitly about the libraries getting linked by deafult, you need to use -v
option to check them. You can also pass -Wl,--verbose
option to get even more verbose output.
If you want to restrict the default linking, you can make use of -nostdlib
switch.

- 133,132
- 16
- 183
- 261