Sunday, February 27, 2011

Setup GLFW 3 library in Codeblocks IDE

GLFW is a free, open-source, multi-platform library for OpenGL and OpenGL ES especially design for game development. OpenGL itself does not provide features to handle window creation and other input, so GLFW is very useful to create the necessary context, manage windows, timing, read and control user input via keyboard, mouse, joysticks and other purposes. There are many other library alternatives to GLFW such as SDL and freeglut but if you are looking best library for 3D game development then GLFW is best. GLFW community is large and very active, at the time of updating this article latest stable version of GLFW is 3.3.2 which is released on January 20, 2020. It comes with documentation, high standard tutorials and guides and lots of test programs.


Here, I will guide you through a step by step procedure to setup GLFW 3 in Codeblocks IDE on Windows 10.

Visit GLFW download page https://www.glfw.org/download.html and download 32 bit/64 bit windows binary files from the Windows pre-compiled binaries section. You have to make sure that bit of your C++ compiler and downloaded GLFW library files should match. Currently, GLFW releases are tested with MinGW, MinGW-w64 and Visual C++ 2010 and above version.

Steps to setup GLFW library and create a project in Code::blocks:
  1. Extract downloaded zip file.
  2. Create a folder in any directory and give a name "GLFW" (eg. c:\GLFW).
  3. Browse extracted files in step 1. Copy "include" folder and paste it inside the directory that you have created in step 2. Copy lib folder of corresponding to your compiler for e.g if you are using minGW then copy lib-mingw folder and paste it inside the directory that you have created in step 2 and rename it to lib. Also copy glfw3.dll file from bin folder to "C:\Windows\System32" if you are using 32-bit library files or copy to "C:\Windows\System" if you are using 64-bit library files.
  4. Now, open the code::blocks and go to File -> New -> Project or click create a new project link from the start page.
  5. From the project wizard select "empty project" and click "Go" button and provide all necessary information. Also, do not forget to add main.cpp file.
  6. Go to the menu bar and select project-> Project properties. A dialogue box will appear and click "Project's build options". Another dialogue box will appear and click the "Search directories" tab. Under the "Compiler" tab add the path of GLFW include folder (e.g. c:\GLFW\include) and under "Linker" tab add the path of GLFW lib folder (e.g. c:\GLFW\lib).
  7. Now go to "Linker Setting" tab and add three linker libglfw3.a, libglfw3dll.a and libopengl32.a. Linker libglfw3.a and libglfw3dll.a are available inside GLFW lib folder and libopengl32.a is available inside minGW compiler lib folder.
Copy and paste below code in main.cpp file and compile it to verify GLFW library setup.

#define GLFW_DLL
#include <GLFW/glfw3.h>
#include <iostream>
#include <stdexcept>

GLFWwindow* window = NULL;
GLFWmonitor* monitor = NULL;

int main(int argc, char *argv[]) {

    if(!glfwInit())
    throw std::runtime_error("glfwInit failed");

    window = glfwCreateWindow(800, 600, "test window",NULL, NULL);
    if(!window)
        throw std::runtime_error("glfwOpenWindow failed.");

   // GLFW settings
    glfwMakeContextCurrent(window);

    while(!glfwWindowShouldClose(window)){
    // process pending events
       glfwPollEvents();
       glClearColor(1, 0, 0, 1); // black
       glClear(GL_COLOR_BUFFER_BIT);
       glfwSwapBuffers(window);
    }
   // clean up and exit
   glfwTerminate();
    return 0;
}

You have to make sure to define GLFW_DLL  and include glfw3.h header always on top of your source code as shown in the above sample code otherwise the compiler will throw undefine reference error.

Please watch the video below for a detailed explanation.


9 comments:

  1. followed this tutorial, tried compiling the code that Code::Blocks inserts as a default. The compile crashed, saying "undefined reference to glfwInit()." I made sure that the glfw library is linked to in the build options, still no luck.

    ReplyDelete
  2. Replies
    1. I had the same problem. I think it was because I was using the 32-bit version of CodeBlocks and 64-bit version of GLFW on 64-bit Windows 7.

      The project compiled properly after I followed this articles steps with the 32-bit version of GLFW.

      Delete
    2. I had the same problem, to sum it up - this worked for me:

      64-bit Windows
      32-bit Codeblocks
      32-bit GLFW

      Delete
    3. ^^----This guy just solved 8 hours of my problems!

      Delete
  3. I can't get it work!!!!!

    Previously I used Dev-C++ and it worked, but i could read everywhere that Code::Blocks is better and up-to-date, so I downloaded and installed it with MinGW.
    I had some problems too, but when I read this tutorial and the comments I could solve some of them, now it works better:

    The compiler writes no errors and also it creates the windows I need, but they are BLACK! IT DRAWS NOTHING!
    I also copied the sample code from glfw.org, it created the window and sill nothing. It was black aswell. So the problem is not in my code.
    Then what can it be? Please help me!

    I have:
    Windows 7 Ultimate 64-bit
    GLFW 3.0.1 32-bit (I tried with 64-bit and it did not work)

    ReplyDelete
  4. Thnx alot! works great.

    ReplyDelete
  5. Thnx alot! works great.

    ReplyDelete