title: Why do you need an explicit `-lm` compiler option
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/4606301/gcc-why-the-lm-flag-is-needed-to-link-the-math-library">gcc: why the -lm flag is needed to link the math library?</a>  </p>
  </blockquote>
  
  
  
  <p>Generally speaking, in order to use any of the math functions apart from including the header file <code>math.h</code> you have to link with the linker option -lm. <code>-l</code> here would mean the linker option to search of the specific library <code>libm.o</code>.</p>
  
  <p>My Question is</p>
  
  <p>Why GCC does not include this library by default? Is it because the library heavily uses math-coprocessor and it requires to add the extra bit of code to initialize the floating point initialization (I may use the wrong terminology here)?</p>
  
  <p><strong>Note</strong></p>
  
  <p>I just reviewed all the answers mentioned in the link <a href="https://stackoverflow.com/questions/4606301/gcc-why-the-lm-flag-is-needed-to-link-the-math-library">http://stackoverflow.com</a>. This doesn't makes much sense to me. There are three basic reasons attributed</p>
  
  <ol>
  <li>The standard libraries are guaranteed to be available. Linking other posix libraries like pthread explicitly makes sense, but why do we have to do an explicit link for a standard library. Even the historical reason is not very clear.</li>
  <li>Why was libm separated from libc? </li>
  <li>Why are we still inheriting these behaviors in the recent gcc compilers? What simplicity it achives? Here is what I tested, without libm and with libm. The One without libm, I have written my own version of Pow</li>
  </ol>
  
  <p>Here is the example</p>
  
  <pre><code>abhibhat@abhibhat-VirtualBox:~/Projects/GIPL6_2$ ls -1 Test_*|xargs -I{} sh -c "echo {} &amp;&amp; echo "-----------------" &amp;&amp; cat {}"
  Test_withlibm.c
  -----------------
  #include&lt;stdio.h&gt;
  #include&lt;math.h&gt;
  int main() {
      int i=20;
      double output1=pow(2.618033988749895,i);
      return 0;
      }
  Test_withoutlibm.c
  -----------------
  #include&lt;stdio.h&gt;
  #include&lt;math.h&gt;
  double Pow(double _X, int _Y)  {
      double _Z = 1;
      for (; _Y; _X *= _X) {
      if (_Y &amp; 1) _Z *= _X;
      _Y &gt;&gt;= 1;
      }
      return _Z; 
      }
  int main() {
      int i=20;
      double output1=Pow(2.618033988749895,i);
      return 0;
      }
  abhibhat@abhibhat-VirtualBox:~/Projects/GIPL6_2$ gcc Test_withlibm.c -lm -o Main_withlibm.o
  abhibhat@abhibhat-VirtualBox:~/Projects/GIPL6_2$ gcc Test_withoutlibm.c -o Main_withoutlibm.o
  abhibhat@abhibhat-VirtualBox:~/Projects/GIPL6_2$ objdump -d Main_withoutlibm.o|wc -l
  261
  abhibhat@abhibhat-VirtualBox:~/Projects/GIPL6_2$ objdump -d Main_withlibm.o|wc -l
  241
  </code></pre>