title: How do you properly link to a static library using g++
body: <blockquote>
    <p>Solution:
    Thanks to everyone who commented on this issue, but I resolved it on another forum, and figured I would post the answer here for anybody having the same issue.</p>
    
    <p>So, I guess only dynamic libraries make use of __declspec(dllexport), so when you try to create a static library, the methods are exported (an the names need to be mangled to be c++ compatible), so when declaring extern "C" __declspec.... you end up with method names that aren't recognized when trying to link statically.</p>
    
    <p>So, simple fix.....remove the __declspec</p>
  </blockquote>
  
  <p>I have 2 projects, one is a static library, the other is just a win32 application.</p>
  
  <p>I simply want to include the library I've created into my win32 application, however g++ keeps giving me this error:</p>
  
  <p>../MyLib/TestClass.h:16: undefined reference to `<em>imp</em>__ZTV9TestClass'</p>
  
  <p>That is the error I get when trying to compile the application, even though that file is part of the library.</p>
  
  <p>I have attempted to create the most simplified version of this project as possible in an attempt to find the error.</p>
  
  <p>Here are the source files for both projects:</p>
  
  <p>MyLib.h - This is the main include file for clients to reference functions in the library</p>
  
  <pre><code>#ifndef MYLIB_H
  #define MYLIB_H
  
  #include "libexport.h"
  #include "TestClass.h"
  
  #endif  /* MYLIB_H */
  </code></pre>
  
  <p>libexport.h - Pretty generic file to define import/export keywords</p>
  
  <pre><code>#ifndef LIBEXPORT_H
  #define LIBEXPORT_H
  
  #ifdef __cplusplus
  extern "C" {
  #endif
  
  #ifdef LIB
  #define DLL_EXPORT __declspec(dllexport)
  #else
  #define DLL_EXPORT __declspec(dllimport)
  #endif
  
  #ifdef __cplusplus
  }
  #endif
  
  #endif  /* LIBEXPORT_H */
  </code></pre>
  
  <p>TestClass.h </p>
  
  <pre><code>#ifndef TESTCLASS_H
  #define TESTCLASS_H
  
  #include "libexport.h"
  
  class DLL_EXPORT TestClass
  {
  public:
      TestClass() {};
      virtual ~TestClass() {};
  
      void TestFunc();
  };
  
  #endif  /* TESTCLASS_H */
  </code></pre>
  
  <p>TestClass.cpp</p>
  
  <pre><code>#define LIB
  
  #include &lt;stdio.h&gt;
  #include "TestClass.h"
  
  void TestClass::TestFunc()
  {
      printf("This function was called from within the library.\n");
  }
  </code></pre>
  
  <p>And lastly, the win32 application that implements the library:</p>
  
  <p>Main.cpp</p>
  
  <pre><code>#include &lt;windows.h&gt;
  #include "../MyLib/MyLib.h"
  
  #pragma comment(lib, "libmylib.a")
  
  int __stdcall WinMain(HINSTANCE hInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpCmdLine,
                        int nCmdShow)
  {
  
      TestClass *myClass = new TestClass();
  
      delete myClass;
      myClass = 0;
  
      return 0;
  }
  </code></pre>
  
  <p>The library compiles with no errors, however, here is the output when compiling the main application:</p>
  
  <pre><code>g++.exe    -c -g -MMD -MP -MF build/Debug/MinGW-Windows/main.o.d -o build/Debug/MinGW-Windows/main.o main.cpp
  mkdir -p dist/Debug/MinGW-Windows
  g++.exe     -mwindows -o dist/Debug/MinGW-Windows/testclient build/Debug/MinGW-Windows/main.o -L../MyLib/dist/Debug/MinGW-Windows -lmylib 
  build/Debug/MinGW-Windows/main.o: In function `TestClass':
  C:\Users\Nick\Documents\NetBeansProjects\TestClient/../MyLib/TestClass.h:16: undefined reference to `_imp___ZTV9TestClass'
  make[2]: Leaving directory `/c/Users/Nick/Documents/NetBeansProjects/TestClient'
  build/Debug/MinGW-Windows/main.o: In function `~TestClass':
  make[1]: Leaving directory `/c/Users/Nick/Documents/NetBeansProjects/TestClient'
  C:\Users\Nick\Documents\NetBeansProjects\TestClient/../MyLib/TestClass.h:17: undefined reference to `_imp___ZTV9TestClass'
  collect2: ld returned 1 exit status
  make[2]: *** [dist/Debug/MinGW-Windows/testclient.exe] Error 1
  make[1]: *** [.build-conf] Error 2
  make: *** [.build-impl] Error 2
  
  
  BUILD FAILED (exit value 2, total time: 1s)
  </code></pre>
  
  <p>Most of the other posts I've seen regarding this topic say that the problem lies in the linking order, but even after adding -lmylib to the beginning of the compiler build line, the same errors persist:</p>
  
  <pre><code>g++.exe -lmylib -mwindows -o dist/Debug/MinGW-Windows/testclient build/Debug/MinGW-Windows/main.o -L../MyLib/dist/Debug/MinGW-Windows -lmylib 
  build/Debug/MinGW-Windows/main.o: In function `TestClass':
  C:\Users\Nick\Documents\NetBeansProjects\TestClient/../MyLib/TestClass.h:16: undefined reference to `_imp___ZTV9TestClass'
  </code></pre>
  
  <p>I really need help on this, I've built many dynamic libraries before using the above code, and it works with no problems, I can't understand why I'm having so much trouble building a simple static library. Any help is greatly appreciated.</p>