2

CodeBlocks' GLFW Project is outdated and works only with GLFW 2.7. I am using the latest version, which is 3.0.4, and trying to link it in CodeBlocks statically (I hope that I am using the correct terminology). I would be really happy if someone told me how to do it step by step. I would also like to create an empty project if possible and do everything manually.

This is the code that I am trying to run:

#include <GLFW/glfw3.h>

int main(void)
{
    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

This is what I have in my "Other linker options": -lmingw32 -lopengl32 -lgdi32 and I also copied GLFW folder that is containing the header files in CodeBlocks/MinGW/include.

This is pretty much all I did and I get the following build log:

-------------- Build: Debug in Initializing OpenGL (compiler: GNU GCC Compiler)---------------

mingw32-g++.exe  -o "bin\Debug\Initializing OpenGL.exe" obj\Debug\main.o  -lmingw32 -lopengl32 -lgdi32  
obj\Debug\main.o: In function `main':
D:/Development/OpenGL/Initializing OpenGL/main.cpp:8: undefined reference to `glfwInit'
D:/Development/OpenGL/Initializing OpenGL/main.cpp:12: undefined reference to `glfwCreateWindow'
D:/Development/OpenGL/Initializing OpenGL/main.cpp:15: undefined reference to `glfwTerminate'
D:/Development/OpenGL/Initializing OpenGL/main.cpp:20: undefined reference to `glfwMakeContextCurrent'
D:/Development/OpenGL/Initializing OpenGL/main.cpp:28: undefined reference to `glfwSwapBuffers'
D:/Development/OpenGL/Initializing OpenGL/main.cpp:31: undefined reference to `glfwPollEvents'
D:/Development/OpenGL/Initializing OpenGL/main.cpp:23: undefined reference to `glfwWindowShouldClose'
D:/Development/OpenGL/Initializing OpenGL/main.cpp:34: undefined reference to `glfwTerminate'
collect2.exe: error: ld returned 1 exit status
Colonel Thirty Two
  • 23,953
  • 8
  • 45
  • 85
  • Add it like any other static library. Your IDE should have documentation about this. – Colonel Thirty Two Oct 21 '14 at 19:45
  • 1
    I think the situation is a little bit more complex here. I don't know what to add in "Other linker options" and as much as I know - the order is also important. OFFTOPIC: I saw a lot of people struggling with the unidentified references while linking GLFW in CodeBlocks and I spent a lot of time researching how to do it and being completely new to linking - why was this question marked as unuseful or unclear. There is not a step by step guide how to do it and someone new would be totally blown off of the suggestions that the GLFW documentation and forums provide. – Людмил Григоров Oct 21 '14 at 20:04
  • Look [here](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix). In this thread hardly all reasons for undefined references are discussed and the correct solution for each of them is given. – BDL Oct 21 '14 at 21:08
  • @narutkataaa I don't think it makes sense to make a guide for every single permutation of library and IDE there is. [Here's the process on codeblocks](http://gfycat.com/SolidEmbellishedGorilla) but that much should be easily researchable from the documentation – PeterT Oct 21 '14 at 21:40
  • @PeterT Sorry If I am asking a stupid question, but I don't have the "build" folder... do I have to compile the library? – Людмил Григоров Oct 21 '14 at 22:45
  • @narutkataaa that's the folder I created as a target for the cmake build. You asked for linking glfw, not how to build glfw, so I assumed you already did that or you just downloaded the pre-build binaries on the website, the file there is "glfw-3.0.4.bin.WIN32\lib-mingw\libglfw3.a" – PeterT Oct 21 '14 at 22:56
  • @narutkataaa also, make sure you are actually using the correct bit version. Just because you are on a 64 bit windows version doesn't mean you actually downloaded a 64-bit version of mingw (the default on the codeblocks website is 32-bit). – PeterT Oct 21 '14 at 22:58
  • @PeterT Oh my... I think I am going to cry, but I do not know if it is happiness or the fact that I was aimlessly trying out things I do not understand for 12 straight hours... Thank you! I did have the 32 bit version and your guess saved me the frustration. Thank you a thousand times! – Людмил Григоров Oct 21 '14 at 23:52

1 Answers1

2

Jigger your project config so that the mingw invocation looks something like this:

x86_64-w64-mingw32-g++.exe -Llib -o bin\Debug\glfw-proj.exe obj\Debug\main.o -lglfw3 -lopengl32 -lgdi32

Note the -lglfw3.

Procedure:

  1. Project -> Build options...
  2. Select the top-level configuration (i.e., not Debug or Release)
  3. In the Search directories -> Compiler tab add the path to your GLFW headers (GLFW/glfw3.h and friend)
  4. In the Search directories -> Linker tab add the path to your GLFW library files (libglfw3.a and friends)
  5. In the Linker settings tab add 3 Link libraries: glfw3, opengl32, and gdi32.
genpfault
  • 51,148
  • 11
  • 85
  • 139
  • I did it, but I still get these reference errors. I am using 64 bit windows and have downloaded the 64 bit GLFW library. Created a new project, then pasted the code from above, selected the top-level configuration and then added the path to the GLFW headers in Search Directories -> Compiler and added the path to the GLFW library files in Search directories -> Linker and then in "Other linker options" added -lglfw3 -lopengl32 -lgdi32. – Людмил Григоров Oct 21 '14 at 21:53
  • Same procedure works using the [TDM64 bundle](http://tdm-gcc.tdragon.net/). You'll want the `Settings -> Compiler -> Toolchain executables` tab. – genpfault Oct 21 '14 at 22:11
  • So I installed the TDM64 bundle and in the toolchain executables I set the C++ compiler to mingw32-g++.exe from TDM-GCC-64\bin and tried to compile code... same reference errors. – Людмил Григоров Oct 21 '14 at 22:35
  • Wait, your TDM64 bundle had a `mingw32-g++.exe` in `C:\TDM-GCC-64\bin`? – genpfault Oct 21 '14 at 22:59
  • A whole bunch of exe files actually. – Людмил Григоров Oct 21 '14 at 23:12
  • @narutkataaa just for future reference the 64 bit TDM g++ executable is usually called `x86_64-w64-mingw32-g++.exe` – PeterT Oct 22 '14 at 00:00