title: How do I source/link external functions in C or C++?
body: <p><strong>EDIT:</strong>  I suppose I should clarify, in case it matters.  I am on a AIX Unix box, so I am using VAC compilers - no gnu compilers.
  <strong>End edit</strong></p>
  
  <hr />
  
  <p>I am pretty rusty in C/C++, so forgive me if this is a simple question.</p>
  
  <p>I would like to take common functions out of a few of my C programs and put them in shared libraries or shared objects.  If I was doing this in perl I would put my subs in a perl module and use that module when needed.</p>
  
  <p>For the sake of an example, let's say I have this function:</p>
  
  <pre><code>int giveInteger()
  {
      return 1034;
  }
  </code></pre>
  
  <p>Obviously this is not a real world example, but if I wanted to share that function, how would I proceed?</p>
  
  <p>I'm pretty sure I have 2 options:</p>
  
  <ol>
  <li>Put my shared function in a file, and have it compile with my main program at compile time.  If I ever make changes to my shared function, I would have to recompile my main program.</li>
  <li>Put my shared function in a file, and compile it as a shared library (if I have my terms correct), and have my main program link to that shared library.  Any changes I make to my shared library (after compiling it) would be integrated into my main program at runtime without re-compiling my main program.</li>
  </ol>
  
  <p>Am I correct on that thinking?</p>
  
  <p>If so, how can I complish either/both of those methods?  I've searched a lot and I seem to find information how how I could have my own program link to someone else's shared library, but not how to create my own shared functions and compile them in a way I can use them in my own program.</p>
  
  <p>Thanks so much!</p>
  
  <p>Brian</p>
  
  <hr />
  
  <p><strong>EDIT:</strong> <h2> Conclusion</h2>
  Thanks everyone for your help!  I thought I would add to this post what is working for me (for dynamic shared libraries on AIX) so that others can benefit:</p>
  
  <p>I compile my shared functions:</p>
  
  <pre><code>xlc -c sharedFunctions.c -o sharedFunctions.o
  </code></pre>
  
  <p>Then make it a shared object:</p>
  
  <pre><code>xlc -qmkshrobj -qexpfile=exportlist sharedFunctions.o
  xlc -G -o libsharedFunctions.so sharedFunctions.o  -bE:exportlist
  </code></pre>
  
  <p>Then link it another program:</p>
  
  <pre><code>xlc -brtl -o mainProgram mainProgram.c  -L. -lsharedFunctions
  </code></pre>
  
  <p>And another comment helped me find this link, which also helped:
  <a href="http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/topic/com.ibm.vacpp7a.doc/proguide/ref/compile_library.htm" rel="nofollow noreferrer">http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/topic/com.ibm.vacpp7a.doc/proguide/ref/compile_library.htm</a></p>
  
  <p>Thanks again to all who helped me out!</p>