title: Linking error: selective static linking of libm.a in GCC
body: <p>I want to selectively link <code>libm.a</code> statically, all the other libraries (<code>libc.so</code> included) dinamically. But if I use the math functions from <code>math.h</code>, it <strong>almost always</strong> fails to link correctly. Why? 
  And why does it work sometimes? (For example if I only use <code>sqrt</code>, <code>fabs</code> or, strangely, <code>tanh</code>, it seems to link correctly)</p>
  
  <p>myscript.sh:</p>
  
  <pre><code>#!/bin/bash
  for i in sqrt tanh sin tan  
  do
       echo "-----$i----------"
       sed "s/ciao/$i/" prova.c &gt;provat.c
       gcc provat.c -fno-builtin -l:libm.a
       [[ $? -eq 0 ]] &amp;&amp; { echo -n "$i(2.0)="; ./a.out; echo " OK!"; }
           echo
  done
  </code></pre>
  
  <p>prova.c:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;math.h&gt;
  int main()
  {
      printf("%f", ciao(2.0));
      return 0;
  }
  </code></pre>
  
  <p>If I run <code>myscript.sh</code>, I can see that <code>sqrt</code> and <code>tanh</code> give no problems. <code>sin</code> and <code>tan</code>, instead, fail to link:</p>
  
  <pre><code>$./myscript.sh
  -----sqrt----------
  sqrt(2.0)=1.414214 OK!
  
  -----tanh----------
  tanh(2.0)=0.964028 OK!
  
  -----sin----------
  /usr/lib/x86_64-linux-gnu/libm-2.27.a(s_sin.o): In function `__sin_ifunc':
  (.text+0x4d42): undefined reference to `_dl_x86_cpu_features'
  /usr/lib/x86_64-linux-gnu/libm-2.27.a(s_sin.o): In function `__cos_ifunc':
  (.text+0x4da2): undefined reference to `_dl_x86_cpu_features'
  collect2: error: ld returned 1 exit status
  
  -----tan----------
  /usr/lib/x86_64-linux-gnu/libm-2.27.a(s_tan.o): In function `__tan_ifunc':
  (.text+0x5782): undefined reference to `_dl_x86_cpu_features'
  collect2: error: ld returned 1 exit status
  </code></pre>
  
  <p>I don't understand these error messages. Can somebody explain what happens? 
  Why can't I link <code>libm.a</code> statically (and the rest dinamically)? And why does it work sometimes? </p>
  
  <p><strong>Note:</strong> I use the <code>-fno-builtin</code> flag to GCC, so that GCC doesn't use any of its builtin functions. So the problem is not there.</p>