Static build on CodeBlocks with mingw

rejamoto wrote on Sunday, May 04, 2014:

Hi,

I’m trying to build a project with the example code from GLFW site and I’m not having much luck building GLFW as a static DLL. I got it working with GLFW as a dynamic library, but not static. Any help would be nice.

Using CodeBlocks 13.12 with Mingw 4.8.1 on Windows 7 64.

I get this when I build.

||=== Build: Debug in GLFW_Quick (compiler: Mingw 4.8.1 x86 pthread rev4) ===|
X:\Programming\C++\OpenGL\GLFW Projects\GLFW_Quick\main.cpp|9|warning: unused parameter 'error' [-Wunused-parameter]|
X:\Programming\C++\OpenGL\GLFW Projects\GLFW_Quick\main.cpp|14|warning: unused parameter 'scancode' [-Wunused-parameter]|
X:\Programming\C++\OpenGL\GLFW Projects\GLFW_Quick\main.cpp|14|warning: unused parameter 'mods' [-Wunused-parameter]|
obj\Debug\main.o||In function `key_callback':|
X:\Programming\C++\OpenGL\GLFW Projects\GLFW_Quick\main.cpp|17|undefined reference to `_imp__glfwSetWindowShouldClose'|
obj\Debug\main.o||In function `main':|
X:\Programming\C++\OpenGL\GLFW Projects\GLFW_Quick\main.cpp|24|undefined reference to `_imp__glfwSetErrorCallback'|
X:\Programming\C++\OpenGL\GLFW Projects\GLFW_Quick\main.cpp|26|undefined reference to `_imp__glfwInit'|
X:\Programming\C++\OpenGL\GLFW Projects\GLFW_Quick\main.cpp|29|undefined reference to `_imp__glfwCreateWindow'|
X:\Programming\C++\OpenGL\GLFW Projects\GLFW_Quick\main.cpp|33|undefined reference to `_imp__glfwTerminate'|
X:\Programming\C++\OpenGL\GLFW Projects\GLFW_Quick\main.cpp|37|undefined reference to `_imp__glfwMakeContextCurrent'|
X:\Programming\C++\OpenGL\GLFW Projects\GLFW_Quick\main.cpp|38|undefined reference to `_imp__glfwSetKeyCallback'|
X:\Programming\C++\OpenGL\GLFW Projects\GLFW_Quick\main.cpp|44|undefined reference to `_imp__glfwGetFramebufferSize'|
X:\Programming\C++\OpenGL\GLFW Projects\GLFW_Quick\main.cpp|46|undefined reference to `glViewport@16'|
X:\Programming\C++\OpenGL\GLFW Projects\GLFW_Quick\main.cpp|47|undefined reference to `glClear@4'|
X:\Programming\C++\OpenGL\GLFW Projects\GLFW_Quick\main.cpp|48|undefined reference to `glMatrixMode@4'|
X:\Programming\C++\OpenGL\GLFW Projects\GLFW_Quick\main.cpp|49|undefined reference to `glLoadIdentity@0'|
X:\Programming\C++\OpenGL\GLFW Projects\GLFW_Quick\main.cpp|50|undefined reference to `glOrtho@48'|
X:\Programming\C++\OpenGL\GLFW Projects\GLFW_Quick\main.cpp|51|undefined reference to `glMatrixMode@4'|
X:\Programming\C++\OpenGL\GLFW Projects\GLFW_Quick\main.cpp|52|undefined reference to `glLoadIdentity@0'|
X:\Programming\C++\OpenGL\GLFW Projects\GLFW_Quick\main.cpp|53|undefined reference to `_imp__glfwGetTime'|
X:\Programming\C++\OpenGL\GLFW Projects\GLFW_Quick\main.cpp|53|undefined reference to `glRotatef@16'|
X:\Programming\C++\OpenGL\GLFW Projects\GLFW_Quick\main.cpp|54|undefined reference to `glBegin@4'|
X:\Programming\C++\OpenGL\GLFW Projects\GLFW_Quick\main.cpp|55|undefined reference to `glColor3f@12'|
X:\Programming\C++\OpenGL\GLFW Projects\GLFW_Quick\main.cpp|56|undefined reference to `glVertex3f@12'|
X:\Programming\C++\OpenGL\GLFW Projects\GLFW_Quick\main.cpp|57|undefined reference to `glColor3f@12'|
X:\Programming\C++\OpenGL\GLFW Projects\GLFW_Quick\main.cpp|58|undefined reference to `glVertex3f@12'|
X:\Programming\C++\OpenGL\GLFW Projects\GLFW_Quick\main.cpp|59|undefined reference to `glColor3f@12'|
X:\Programming\C++\OpenGL\GLFW Projects\GLFW_Quick\main.cpp|60|undefined reference to `glVertex3f@12'|
X:\Programming\C++\OpenGL\GLFW Projects\GLFW_Quick\main.cpp|61|undefined reference to `glEnd@0'|
X:\Programming\C++\OpenGL\GLFW Projects\GLFW_Quick\main.cpp|62|undefined reference to `_imp__glfwSwapBuffers'|
X:\Programming\C++\OpenGL\GLFW Projects\GLFW_Quick\main.cpp|63|undefined reference to `_imp__glfwPollEvents'|
X:\Programming\C++\OpenGL\GLFW Projects\GLFW_Quick\main.cpp|40|undefined reference to `_imp__glfwWindowShouldClose'|
X:\Programming\C++\OpenGL\GLFW Projects\GLFW_Quick\main.cpp|66|undefined reference to `_imp__glfwDestroyWindow'|
X:\Programming\C++\OpenGL\GLFW Projects\GLFW_Quick\main.cpp|68|undefined reference to `_imp__glfwTerminate'|
||=== Build failed: 30 error(s), 3 warning(s) (0 minute(s), 0 second(s)) ===|

rejamoto wrote on Sunday, May 04, 2014:

I got rid of the errors by reverting back my settings before I started getting the static library to work. I would be grateful if someone explain or provide a step-by-step on setting up CodeBlocks to build GLFW as a static library.

arampl wrote on Sunday, May 04, 2014:

  1. Download CMake for Windows http://www.cmake.org/cmake/resources/software.html

  2. Install it and run.

  3. Set path to GLFW source (root of source tarball, not folder “src”).

  4. Set path to CodeBlocks project file (where you want it to be created).

  5. Press “Configure” and choose generator “CodeBlocks MinGW makefiles”.

  6. Press “Generate”.

  7. Open generated project in CodeBlocks.

  8. Here, in source tree, you now have library, all examples and tests.

  9. After build you will have static library - “libglfw3.a” (in the “src”) and executables for examples and tests.

Tested with CodeBlocks 12.11 on Windows 7 64

rejamoto wrote on Sunday, May 04, 2014:

I get a CMake Error after I selected CodeBlocks MinGW makefiles.

CMake Error: CMake was unable to find a build program corresponding to "MinGW Makefiles".  CMAKE_MAKE_PROGRAM is not set.  You probably need to select a different build tool.
CMake Error: CMake was unable to find a build program corresponding to "MinGW Makefiles".  CMAKE_MAKE_PROGRAM is not set.  You probably need to select a different build tool.
CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_C_COMPILER_ENV_VAR
CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_C_COMPILER
CMake Error: Could not find cmake module file:D:/Documents/GL/CMakeFiles/2.8.11.2/CMakeCCompiler.cmake
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
Configuring incomplete, errors occurred!

Edit:
I reinstalled CMake and it’s working. Thank you for the help.