1

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?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Adam Jones
  • 47
  • 4

1 Answers1

3

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 library libm. 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.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261