title: Undefined reference to 'function'; linking library does not work
body: <p>g++ can't find a certain function definition, even though it's clearly defined in a included library.</p>
  
  <hr>
  
  <p><strong>main.cpp</strong></p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;StormLib.h&gt;
  
  int main() 
  {
      HANDLE hMpq = NULL;
      SFileOpenArchive("file_name", 0, MPQ_OPEN_READ_ONLY, &amp;hMpq);
  
      return 0;
  }
  </code></pre>
  
  <p><strong>StormLib.h</strong></p>
  
  <pre class="lang-cpp prettyprint-override"><code>// ...
  bool   WINAPI SFileOpenArchive(const TCHAR * szMpqName, DWORD dwPriority, DWORD dwFlags, HANDLE * phMpq);
  // ...
  </code></pre>
  
  <p><strong>Compiling</strong></p>
  
  <pre><code>$ g++ main.cpp 
  /usr/bin/ld: /tmp/ccNSvuaq.o: in function `main':
  main.cpp:(.text+0x4b): undefined reference to `SFileOpenArchive'
  collect2: error: ld returned 1 exit status
  </code></pre>
  
  <hr>
  
  <p>As you can see, <code>SFileOpenArchive()</code> is clearly defined in <code>StormLib.h</code>, and the header file is found by g++, but it still says <code>SFileOpenArchive()</code> is not defined.</p>
  
  <p>This seems to be a commonly asked question on StackOverflow, and most solutions tell me to include the library in the compile command. This is what I've tried:</p>
  
  <pre><code>$ g++ main.cpp /usr/local/include/StormLib.h
  $ g++ -o test main.cpp /usr/local/include/StormLib.h
  </code></pre>
  
  <p>Both commands give the same error as the first.</p>
  
  <p>The most similar question with an answer is <a href="https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix/12574403#12574403">this one with over 1400 votes</a>, but it does not solve my problem.</p>
  
  <p>I'm new to C++ so this likely has a simple solution.</p>