title: Creating libraries in C/C++ - ld can't find my library
body: <p>I'm trying to learn how to create a C/C++ library in a linux environment but I'm having a problem (probably a trivial one) that online tutorials had not helped to solve. </p>
  
  <p>For definiteness let's say I have a foo.c file with the following code:</p>
  
  <pre><code>//file: foo.c
  #include &lt;stdio.h&gt;
  void hello(void)
  {
    printf("hello!\n");
  }
  </code></pre>
  
  <p>a foo.h:</p>
  
  <pre><code>//file: foo.h
  void hello(void);
  </code></pre>
  
  <p>and a program that uses the function hello() from foo.c, named prog.c:</p>
  
  <pre><code>//file: prog.c
  
  #include "foo.h"
  
  int main(void)
  {
    hello();
    return 0;
  }
  </code></pre>
  
  <p>The three files are all on the same directory. Then I compiled foo.c with:</p>
  
  <pre><code>gcc -fPIC -c foo.c
  </code></pre>
  
  <p>and got a foo.o file. Then I used ld to create the library file:</p>
  
  <pre><code>ld -G foo.o -o libfoo.so
  </code></pre>
  
  <p>But when I try to compile prog.c with:</p>
  
  <pre><code>gcc -o prog prog.c -lfoo
  </code></pre>
  
  <p>I got an error message:</p>
  
  <pre><code>/usr/bin/ld: cannot find -lfoo
  collect2: ld returned 1 exit status
  </code></pre>
  
  <p>I'm convinced that this is some kind of trivial path problem, but I couldn't find the solution. So my question is really if this procedure above is wrong or if I have to put the libfoo.so file in a special path. </p>
  
  <p>Another question is how this changes if I'm using g++ instead of gcc.</p>
  
  <p>Thanks.</p>
  
  <p>EDIT:</p>
  
  <p>I know I can compile both prog.c and foo.c to prog.o and foo.o an then link them to make an executable. But in my original problem I want to compile foo.c in a way that I can distribute to people who will use my functions in their own programs.</p>