title: LD: Linking a shared library to a static library
body: <p>I am trying to link a <code>static library</code> with <code>shared library</code> and this throws me an error saying <code>recompile with -fPIC</code></p>
  
  <p>Here's what I have tried using an example:</p>
  
  <pre><code>savari@Ramana:~/Junk$ cat common.h 
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  void func1(int *p);
  void func2();
  </code></pre>
  
  <p>Shared library code:</p>
  
  <pre><code>savari@Ramana:~/Junk$ cat shared.c 
  #include "common.h"
  
  void func2()
  {
      int i=10;
      func1(&amp;i);
  }
  </code></pre>
  
  <p>And the static library code:</p>
  
  <pre><code>#include "common.h"
  
  int k;
  
  void func1(int *p)
  {
      printf("%d\n", *p);
  }
  </code></pre>
  
  <p>Now, see that the shared library uses the API of static library.</p>
  
  <p>Here's how I compiled:</p>
  
  <pre><code>gcc -c static.c
  
  ar rcs libStatic.a static.o
  
  gcc -c shared.c
  
  gcc -shared -fPIC -o libShared.so shared.o  -L. -lStatic
  </code></pre>
  
  <p>After the last command, I get the following error:</p>
  
  <pre><code>/usr/bin/ld: ./libStatic.a(static.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
  ./libStatic.a: error adding symbols: Bad value
  collect2: error: ld returned 1 exit status
  </code></pre>
  
  <p>I actually got <code>static library</code> from a vendor and I am trying to build a <code>shared library</code> on top of it. I don't have the source of static library.</p>
  
  <p>I get other type of error saying:</p>
  
  <pre><code>relocation R_ARM_THM_MOVW_ABS_NC against `a local symbol' can not be used when making a shared object; recompile with -fPIC
  error adding symbols: Bad value
  collect2: error: ld returned 1 exit status
  </code></pre>
  
  <p>I went through so many articles, but couldn't able to figure out. Please help me fix this.</p>
  
  <p>References:</p>
  
  <p><a href="http://www.kaizou.org/2015/01/linux-libraries/" rel="nofollow noreferrer">Reference-1</a></p>
  
  <p><a href="https://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc">Reference-2</a></p>