title: g++ shared library not found same directory with -L
body: <p>I just cant see what Im doing wrong here:</p>
  
  <p><strong>Q. Why is my library not being found by g++ when compiling the program with a shared object?</strong></p>
  
  <p>Im trying to include a shared library to my c++ program:</p>
  
  <pre><code>g++ -fpic -c sha.cpp
  g++ -shared -o libsha.so sha.o
  g++ main.cpp -o main -L. -lsha
  </code></pre>
  
  <p>where sha.cpp and sha.h are the library files and main.cpp is my program.</p>
  
  <p>Ive tried the same with static library which works find:</p>
  
  <pre><code>g++ -static -c sha.cpp -o libsha.o
  ar rcs libsha.a libsha.o
  g++ main.cpp -o main -L. -lsha
  </code></pre>
  
  <p>The platform is cygwin on windows and here is the output:</p>
  
  <pre><code>rob@pc /cygdrive/c/src/a
  $ g++ main.cpp -o shatest -L. -lsha
  /usr/lib/gcc/i686-pc-cygwin/4.3.4/../../../../i686-pc-cygwin/bin/ld: cannot find -lsha
  collect2: ld returned 1 exit status
  </code></pre>
  
  <p>Ive read all forum posts but the library is in the same folder!</p>
  
  <pre><code>$ ls
  libsha.so  main.cpp  sha.cpp  sha.h  sha.o
  </code></pre>
  
  <p>The reason im doing this is that on another platform, a library is being created whereby when one object is called, it works, but the app crashes when the second object is being constructed. Im doing the above as a simple test! (annoyingly not so simple).</p>
  
  <p>source files below:</p>
  
  <p>main.cpp</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;pthread.h&gt;
  #include "sha.h"
  
  void *thread_one( void *ptr );
  void *thread_two( void *ptr );
  
  main()
  {
       pthread_t thread1, thread2;
       int  iret1, iret2;
  
       /* Create independent threads each of which will execute function */
       iret1 = pthread_create( &amp;thread1, NULL, thread_one, 0);
       iret2 = pthread_create( &amp;thread2, NULL, thread_two, 0);
  
       /* Wait till threads are complete before main continues. Unless we  */
       /* wait we run the risk of executing an exit which will terminate   */
       /* the process and all threads before the threads have completed.   */
       pthread_join( thread1, NULL);
       pthread_join( thread2, NULL); 
  
       printf("Thread 1 returns: %d\n",iret1);
       printf("Thread 2 returns: %d\n",iret2);
       exit(0);
  }
  
  void *thread_one( void *ptr )
  {
      printf("Run thread_one\n");
      CObj1 obj;
  }
  
  void *thread_two( void *ptr )
  {
      printf("Run thread_two\n");
      CObj2 obj;
  }
  </code></pre>
  
  <p>sha.cpp</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  #include "sha.h"
  
  CObj1::CObj1()
  {
      printf("CObj1\n");
      a = 10;
      printf("CObj1: %d \n", a);
  }
  
  CObj2::CObj2()
  {
      printf("CObj2\n");
      a = 10;
      printf("CObj2: %d \n", a);
  }
  </code></pre>
  
  <p>sha.h</p>
  
  <pre><code>#ifndef LIB
  #define LIB
  
  class CObj1
  {
  public:
      CObj1();
  
  private:
      int a;
  };
  
  class CObj2
  {
  public:
      CObj2();
  
  private:
      int a;
  };
  
  #endif
  </code></pre>