title: Shared library from archive (.a) file in C
body: <p>I was given a task to create archive file <strong>.a</strong> from objects file and create a shared library file from archive .a file. I have tried experiment having following file:<br/>
  <strong>foo.h</strong></p>
  
  <pre><code>#ifndef _foo_h__
  #define _foo_h__
  extern void foo(void);
  extern void bar(void); 
  #endif //_foo_h__
  </code></pre>
  
  <p><br/>
  <strong>foo.c</strong></p>
  
  <pre><code>#include&lt;stdio.h&gt;
  
  void foo(void)
  {
      puts("Hello, I'm a shared library");
  }
  </code></pre>
  
  <p><br/>
  <strong>bar.c</strong></p>
  
  <pre><code>#include&lt;stdio.h&gt;
  
  void bar(void)
  {
      puts("This is bar function call.");
  }
  </code></pre>
  
  <p><br/>
  <strong>main.c</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include"foo.h"
  
  int main(void)
  {
      puts("This is a shared library test...");
      foo();
      bar();
      return 0;
  }
  </code></pre>
  
  <p><br/>
  <strong>Makefile</strong></p>
  
  <pre><code>CFLAGS = -Wall -Werror
  LDFLAGS = -L/home/betatest/Public/implicit-rule-archive -Wl,-rpath,'$$ORIGIN'
  
  all : run
  
  run : main.o libfoo.so
      $(CC) $(LDFLAGS) -o $@ $^
  
  libfoo.so : CFLAGS += -fPIC # Build objects for .so with -fPIC.
  libfoo.so : libfoo.a
      $(CC) -shared -o $@ $^
  
  libfoo.a : foo.o bar.o
      ar cvq libfoo.a foo.o bar.o
  #   ar cr libfoo.a foo.o bar.o
  
  # Compile any .o from .c. Also make dependencies automatically.
  %.o : %.c
      $(CC) -c $(CFLAGS) -o $@ $&lt;
  
  #Include dependencies on subsequent builds.
  
  .PHONY : all clean
  
  clean :
      -rm -f *.o *.d run libfoo.*
  </code></pre>
  
  <p>This simple test program seems to run fine but while compiling using make it producing error as:</p>
  
  <pre><code>cc -c -Wall -Werror -o main.o main.c
  cc -c -Wall -Werror -fPIC  -o foo.o foo.c
  cc -c -Wall -Werror -fPIC  -o bar.o bar.c
  ar cvq libfoo.a foo.o bar.o
  a - foo.o
  a - bar.o
  cc -shared -o libfoo.so libfoo.a
  cc -L/home/betatest/Public/implicit-rule-archive -Wl,-rpath,'$ORIGIN' -o run main.o libfoo.so
  main.o: In function `main':
  main.c:(.text+0xf): undefined reference to `foo'
  main.c:(.text+0x14): undefined reference to `bar'
  collect2: ld returned 1 exit status
  Makefile-test:7: recipe for target 'run' failed
  make: *** [run] Error 1
  </code></pre>
  
  <p>Somebody please point out that where I am getting wrong?? Thanks a lot.</p>