About -lm switch used for linking math.h

Tim Prince n8tm@aol.com
Fri Jan 27 14:55:00 GMT 2012


  On 01/26/2012 04:57 PM, Avinash Sonawane wrote:
> Sir,
> I am using gcc 4.6.1.
> I have few questions regarding -lm switch. Please kindly guide me for the same.
>
> 1) Why I need to use -lm flag for math.h even if I had declared the
> #include<math.h>  in the code?
> 2) If it's all about linking the library then why I don't have to do
> the same thing with stdio.h too?
> 3) Or we have to add the -l'library' switch for every library excluding stdio.h?
> 4) In case if the -l'library' switch is used for some specific
> libraries only then please direct me to the complete list of such
> libraries.
>
> Thanks.
>
> --
> Avinash Sonawane
> PICT Pune
> India
1.  This depends on which gcc language you use.  Requirement to set -lm 
explicitly is traditional "legacy" for C, but not for e.g. fortran, 
C++,....  It's unusual for a compiler to keep a list of headers you 
include and figure out the corresponding libraries required at link time.
2. <stdio.h> library support is always searched in C programs, 
regardless of your use of the header.
4.  As gcc itself doesn't supply many of the standard C library 
functions, you must use the facilities of your implementation (possibly 
linux, possibly something entirely different) to see which libraries are 
available.  gcc -v may show you which libraries are searched.

-- 
Tim Prince



More information about the Gcc-help mailing list