[prog] using a -ln option with GCC when compiling c code with math.h

Wolfgang Petzold petzold at villa-chaos.de
Mon Jun 23 10:18:20 EST 2003


Guru -, 23.06.03:

> why is it necessary to use the -ln flag with GCC when compiling something
> that uses math.h?

Hi!

A short answer, together with a related question of mine:

The "-l<anything>" flag causes gcc --during the linking stage-- to link a
library called "lib<anything>.<someextension>". Assuming you made a typo
and really meant "-lm" this means that you have to use the "libm.*" (m for
math) library when using math functions. The header files describe what
functions there are and how to use them, but the function implementations
are in the libraries.

So, my (and maybe your) open question is: How do I find out what libraries
I have to use? The man pages --sin(3) for instance-- don't tell me I would
have to use "libm" -- and not, say, "libmath" via a "-lmath" flag. (Well,
I do know that standard libraries should be well described in various
books and tutorials on C, but how about this in the general case?)

Wolfgang



More information about the Programming mailing list