title: Dynamically Linking GLEW with CMake
body: <p>I've looked over Stack Overflow and Google to try to figure this out, but everything I've seen regarding a dynamic linking either doesn't work or they end up static linking. </p>
  
  <pre><code>E:/CLion/GLEW-Test/main.cpp:8: undefined reference to `_imp__glewInit@0'
  E:/CLion/GLEW-Test/main.cpp:12: undefined reference to `_imp__glewGetErrorString@4'
  E:/CLion/GLEW-Test/main.cpp:14: undefined reference to `_imp__glewGetString@4'
  </code></pre>
  
  <p>I am trying to dynamically link GLEW using CLion. The problem is, I've grown up with Visual Studio so I've never really had to think much about it. I'm trying to teach myself CMake and OpenGL, so this would have been a perfect exercise.</p>
  
  <p>I downloaded the source for GLEW and built it with CMake and MinGW. I placed the resulting <code>glew32.dll</code> in <code>C:/Windows/System32/</code>, and I placed the <code>libglew32.a</code> in <code>C:/MinGW/lib</code>, and I placed <code>glew.h</code>, <code>glxew.h</code>, and <code>wglew.h</code> into <code>C:/MinGW/include/GL</code>. </p>
  
  <p>Currently, I also have the libraries <code>glew32.dll</code>, <code>libglu32.a</code>, and <code>libopengl32.a</code> in my build directory.</p>
  
  <p>Here is my CMakeLists.txt:</p>
  
  <pre><code>cmake_minimum_required(VERSION 3.8)
  project(GLEW_Test)
  
  set(CMAKE_CXX_STANDARD 11)
  set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
  set( CMAKE_VERBOSE_MAKEFILE on )
  
  set(SOURCE_FILES main.cpp)
  add_executable(GLEW_Test ${SOURCE_FILES})
  target_link_libraries(GLEW_Test glew32 glu32 opengl32)
  </code></pre>
  
  <p>and this is my main.cpp:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;GL/glew.h&gt;
  
  int main()
  {
      std::cout &lt;&lt; "Hello, World!" &lt;&lt; std::endl;
  
      GLenum err = glewInit();
      if (GLEW_OK != err)
      {
          /* Problem: glewInit failed, something is seriously wrong. */
          fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
      }
      fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
  
      return 0;
  }
  </code></pre>
  
  <p>Now, if I <code>#define GLEW_STATIC</code> before I <code>#include &lt;GL/glew.h&gt;</code>, the program compiles and outputs the expected output (An error for having no GLContext).    However, to my knowledge, that is static linking, and I want to dynamically link GLEW.  This is what I see when I compile without <code>#define GLEW_STATIC</code>:</p>
  
  <pre><code>"C:\Program Files\JetBrains\CLion 2017.2.2\bin\cmake\bin\cmake.exe" --build E:\CLion\GLEW-Test\cmake-build-debug --target GLEW_Test -- -j 2
  "C:\Program Files\JetBrains\CLion 2017.2.2\bin\cmake\bin\cmake.exe" -HE:\CLion\GLEW-Test -BE:\CLion\GLEW-Test\cmake-build-debug --check-build-system CMakeFiles\Makefile.cmake 0
  C:/MinGW/bin/mingw32-make.exe -f CMakeFiles\Makefile2 GLEW_Test
  mingw32-make.exe[1]: Entering directory 'E:/CLion/GLEW-Test/cmake-build-debug'
  "C:\Program Files\JetBrains\CLion 2017.2.2\bin\cmake\bin\cmake.exe" -HE:\CLion\GLEW-Test -BE:\CLion\GLEW-Test\cmake-build-debug --check-build-system CMakeFiles\Makefile.cmake 0
  "C:\Program Files\JetBrains\CLion 2017.2.2\bin\cmake\bin\cmake.exe" -E cmake_progress_start E:\CLion\GLEW-Test\cmake-build-debug\CMakeFiles 2
  C:/MinGW/bin/mingw32-make.exe -f CMakeFiles\Makefile2 CMakeFiles/GLEW_Test.dir/all
  mingw32-make.exe[2]: Entering directory 'E:/CLion/GLEW-Test/cmake-build-debug'
  C:/MinGW/bin/mingw32-make.exe -f CMakeFiles\GLEW_Test.dir\build.make CMakeFiles/GLEW_Test.dir/depend
  mingw32-make.exe[3]: Entering directory 'E:/CLion/GLEW-Test/cmake-build-debug'
  "C:\Program Files\JetBrains\CLion 2017.2.2\bin\cmake\bin\cmake.exe" -E cmake_depends "MinGW Makefiles" E:\CLion\GLEW-Test E:\CLion\GLEW-Test E:\CLion\GLEW-Test\cmake-build-debug E:\CLion\GLEW-Test\cmake-build-debug E:\CLion\GLEW-Test\cmake-build-debug\CMakeFiles\GLEW_Test.dir\DependInfo.cmake --color=
  mingw32-make.exe[3]: Leaving directory 'E:/CLion/GLEW-Test/cmake-build-debug'
  C:/MinGW/bin/mingw32-make.exe -f CMakeFiles\GLEW_Test.dir\build.make CMakeFiles/GLEW_Test.dir/build
  mingw32-make.exe[3]: Entering directory 'E:/CLion/GLEW-Test/cmake-build-debug'
  [ 50%] Linking CXX executable GLEW_Test.exe
  "C:\Program Files\JetBrains\CLion 2017.2.2\bin\cmake\bin\cmake.exe" -E cmake_link_script CMakeFiles\GLEW_Test.dir\link.txt --verbose=1
  "C:\Program Files\JetBrains\CLion 2017.2.2\bin\cmake\bin\cmake.exe" -E remove -f CMakeFiles\GLEW_Test.dir/objects.a
  C:\MinGW\bin\ar.exe cr CMakeFiles\GLEW_Test.dir/objects.a @CMakeFiles\GLEW_Test.dir\objects1.rsp
  C:\MinGW\bin\g++.exe  -Wall -Wextra -g   -Wl,--whole-archive CMakeFiles\GLEW_Test.dir/objects.a -Wl,--no-whole-archive  -o GLEW_Test.exe -Wl,--out-implib,libGLEW_Test.dll.a -Wl,--major-image-version,0,--minor-image-version,0 @CMakeFiles\GLEW_Test.dir\linklibs.rsp
  CMakeFiles\GLEW_Test.dir/objects.a(main.cpp.obj): In function `main':
  E:/CLion/GLEW-Test/main.cpp:8: undefined reference to `_imp__glewInit@0'
  E:/CLion/GLEW-Test/main.cpp:12: undefined reference to `_imp__glewGetErrorString@4'
  E:/CLion/GLEW-Test/main.cpp:14: undefined reference to `_imp__glewGetString@4'
  collect2.exe: error: ld returned 1 exit status
  mingw32-make.exe[3]: *** [GLEW_Test.exe] Error 1
  CMakeFiles\GLEW_Test.dir\build.make:98: recipe for target 'GLEW_Test.exe' failed
  mingw32-make.exe[2]: *** [CMakeFiles/GLEW_Test.dir/all] Error 2
  mingw32-make.exe[3]: Leaving directory 'E:/CLion/GLEW-Test/cmake-build-debug'
  mingw32-make.exe[1]: *** [CMakeFiles/GLEW_Test.dir/rule] Error 2
  CMakeFiles\Makefile2:69: recipe for target 'CMakeFiles/GLEW_Test.dir/all' failed
  mingw32-make.exe[2]: Leaving directory 'E:/CLion/GLEW-Test/cmake-build-debug'
  CMakeFiles\Makefile2:81: recipe for target 'CMakeFiles/GLEW_Test.dir/rule' failed
  mingw32-make.exe[1]: Leaving directory 'E:/CLion/GLEW-Test/cmake-build-debug'
  Makefile:120: recipe for target 'GLEW_Test' failed
  mingw32-make.exe: *** [GLEW_Test] Error 2
  </code></pre>
  
  <p>What am I doing wrong? I followed the guide at <a href="http://glew.sourceforge.net/install.html" rel="nofollow noreferrer">glew's sourceforge</a>, and placed the .dll in my System32 folder. Since I built with CMake and MinGW, I have .a files instead of .lib files, and I stuck those in my MinGW/lib directory. 
  <strong>The only thing I didn't do (because I wasn't sure), was do nothing with my libglew32.dll.a</strong>
  Is this the culprit? I'm not sure what to do with it, and googling for information on a .dll.a file turned up nothing.  </p>