title: GCC linking libc static and some other library dynamically, revisited?
body: <p>The following questions are relevant but do not answer my question:</p>
  
  <p><a href="https://stackoverflow.com/questions/26161332/linking-partially-static-and-partially-dynamic-in-gcc">Linking partially static and partially dynamic in GCC</a></p>
  
  <p><a href="https://stackoverflow.com/questions/10432968/linking-a-dynamic-library-to-a-static-library-that-links-to-other-static-librari">Linking a dynamic library to a static library that links to other static libraries</a></p>
  
  <p><a href="https://stackoverflow.com/questions/4156055/gcc-static-linking-only-some-libraries">GCC: static linking only some libraries</a></p>
  
  <p><a href="https://stackoverflow.com/questions/725472/static-link-of-shared-library-function-in-gcc">Static link of shared library function in gcc</a></p>
  
  <p>I asked a very similar question earlier, but since the previous question started by me got somewhat cluttered in the comment section and not fully answered (but I flagged it as answered since it was a good effort and did at least partially answer it) I will ask a new question. The question is specifically how to link libc as static, while linking some other library (e.g. libm) dynamically. This was suggested that cannot be done in the first question, is that true? If so it would be very interesting to know why not.</p>
  
  <p>Is it even possible to do this? Someone made a comment (which was removed for some reason, maybe it was incorrect?) that it is possible, but there must then <em>also</em> exist a dynamically linked version of libc, since it will be required by the dynamic library (e.g. dynamic libm will require dynamic libc (?)).</p>
  
  <p>This is fine for me, but it is not obvious to me how to tell GCC to do this, i.e. link in libc as both static and dynamic. How do I do this (I made a couple attempts, some are shown later in the question)? Or is there some other way to do what I want?</p>
  
  <p>We first see that by simply running gcc test.c -lm, everything is linked in dynamically, as follows:</p>
  
  <pre><code>$ gcc test.c -lm
  $ ldd a.out 
          linux-vdso.so.1 (0x00007fffb37d1000)
          libm.so.6 =&gt; /lib64/libm.so.6 (0x00007f3b0eeb6000)
          libc.so.6 =&gt; /lib64/libc.so.6 (0x00007f3b0eb10000)
          /lib64/ld-linux-x86-64.so.2 (0x00007f3b0f1b0000)
  </code></pre>
  
  <p>To link only libm as static, while allowing libc to remain dynamic, we can do (as Z boson pointed out in one of the aforementioned questions):</p>
  
  <pre><code>$ gcc test.c /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../lib64/libm.a
  
  $ ldd a.out 
          linux-vdso.so.1 (0x00007fff747ff000)
          libc.so.6 =&gt; /lib64/libc.so.6 (0x00007f09aaa0c000)
          /lib64/ld-linux-x86-64.so.2 (0x00007f09aadb2000)
  </code></pre>
  
  <p>However, attempting the same procedure to link libc static and libm dynamic, does not seem to work:</p>
  
  <pre><code>$ gcc test.c /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../lib64/libc.a -lm
  /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../x86_64-pc-linux-gnu/bin/ld: dynamic STT_GNU_IFUNC symbol `strcmp' with pointer equality in `/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../lib64/libc.a(strcmp.o)' can not be used when making an executable; recompile with -fPIE and relink with -pie
  collect2: error: ld returned 1 exit status
  </code></pre>
  
  <p>What does this error message mean?</p>
  
  <p>Some other attempts (most were also included in my first question):</p>
  
  <pre><code>$ gcc test.c /usr/lib64/libc.a
  linux-gnu/4.7.3/../../../../x86_64-pc-linux-gnu/bin/ld: dynamic STT_GNU_IFUNC symbol `strcmp' with pointer equality in `/usr/lib64/libc.a(strcmp.o)' can not be used when making an executable; recompile with -fPIE and relink with -pie
  urned 1 exit status
  $ gcc test.c -Wl,-Bdynamic -lm -Wl,-Bstatic -lc
  /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../x86_64-pc-linux-gnu/bin/ld: cannot find -lgcc_s
  /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../x86_64-pc-linux-gnu/bin/ld: cannot find -lgcc_s
  collect2: error: ld returned 1 exit status
  $ gcc -Wl,-Bdynamic -lm -Wl,-Bstatic -lc test.c
  /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../x86_64-pc-linux-gnu/bin/ld: cannot find -lgcc_s
  /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../x86_64-pc-linux-gnu/bin/ld: cannot find -lgcc_s
  collect2: error: ld returned 1 exit status
  $ gcc -Wl,-Bstatic -lc -Wl,-Bdynamic -lm test.c
  /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../x86_64-pc-linux-gnu/bin/ld: dynamic STT_GNU_IFUNC symbol `strcmp' with pointer equality in `/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../lib64/libc.a(strcmp.o)' can not be used when making an executable; recompile with -fPIE and relink with -pie
  collect2: error: ld returned 1 exit status
  $ gcc test.c /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../lib64/libc.a /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../lib64/libc.so -lm
  /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../x86_64-pc-linux-gnu/bin/ld: dynamic STT_GNU_IFUNC symbol `strcmp' with pointer equality in `/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../lib64/libc.a(strcmp.o)' can not be used when making an executable; recompile with -fPIE and relink with -pie
  collect2: error: ld returned 1 exit status
  $ gcc test.c /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../lib64/libc.so /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../lib64/libc.a -lm
  </code></pre>
  
  <p>Note that the last one compiled/linked successfully. However libc has not been linked in statically, only dynamically, so it is another failed attempt.</p>
  
  <p>The test program is simply the following:</p>
  
  <pre><code>$ cat test.c 
  #include &lt;stdio.h&gt;
  #include &lt;math.h&gt;
  
  int main(int argc, char **argv)
  {
          int i;
          int result;
  
          for(i = 0; i &lt; 65535; i++) {
                  result = sin(i);
          }
  
          return 0;
  }
  </code></pre>
  
  <p>Edit:</p>
  
  <p>I've also tried statifier and ermine, as suggested in this question:</p>
  
  <p><a href="https://stackoverflow.com/questions/725472/static-link-of-shared-library-function-in-gcc">Static link of shared library function in gcc</a></p>
  
  <p>Neither works.</p>