Search code examples
cgcclinkerglfw

How do I link to GLFW using gcc on windows?


I'm trying to link to GLWF using GCC on Windows.

I built the code locally on my computer, took the include and lib directories and deleted the rest of the files. Now I'm trying to link them to my code using a batch file and the gcc compiler.

build.bat:

@echo off

SET COMPILER_FLAGS= -I./Dependencies/GLFW/Include -I./Dependencies/GLFW/lib
SET LINKER_FLAGS= -lopengl32 -lglfw3 

gcc %COMPILER_FLAGS% -o foo.exe core/main.c %LINKER_FLAGS%

main.c:

#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 */
        glClear(GL_COLOR_BUFFER_BIT);

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

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

    glfwTerminate();
    return 0;
}

File path:

enter image description here

Errors

When including the -lglfw3 flag in build.bat I get error:

cannot find -lglfw3

When removing the -lglfw3 flag in build.bat I get errors:

undefined reference to `glfwInit'

undefined reference to `glfwCreateWindow'

undefined reference to `glfwTerminate'

undefined reference to `glfwMakeContextCurrent'

undefined reference to `glfwSwapBuffers'

undefined reference to `glfwPollEvents'

undefined reference to `glfwWindowShouldClose'

undefined reference to `glfwWindowShouldClose'

undefined reference to `glfwTerminate'

When using -L./Dependencies/GLFW/lib in the linker flag instead of -I

Warning: corrupt .drectve at end of def file (x21)

undefined reference to `__security_cookie'

undefined reference to `_RTC_CheckStackVars'

many other undefined mingw32 references

I've looked all over the place and I have run into similar problems, but I really didn't understand the issue wherever I went. I saw a Reddit post about Arch Linux talking about how the linker automatically searches for folders that start with lib to add the .lib files in, and how you could manually set that for shell scripts. I tried that and it didn't work I don't know if it's because it's a bat file because I'm using Windows and not a shell file like Unix systems, or if it's because the problem is elsewhere. Another potential issue might be that I compiled the GLFW binaries with CMAKE which I believe uses CL but I'm not sure and now I'm trying to link with GCC and there are some problems with compatibility/security because of that.


Solution

  • This:

    -I./Dependencies/GLFW/Include
    

    tells your compiler to search for the header files that you #include in your source code in the directory ./Dependencies/GLFW/Include.

    And this:

    -I./Dependencies/GLFW/lib
    

    tells it also to look for header files in ./Dependencies/GLFW/lib But you haven't got any header files there. You've got (presumably) the binary libraries that the linker needs, including (presumably) the opengl32 and glfw3 libraries.

    You have to tell gcc where the linker should search for these, and you need to tell it that it's a place to search for libraries, not header files. This is so that gcc will pass on this information to the linker (ld), which needs it, and not to the C compiler (cc1), which doesn't. You do that with the -L <dir> option. See GCC manual:3.16 Options for Directory Search

    Change:

    SET COMPILER_FLAGS= -I./Dependencies/GLFW/Include -I./Dependencies/GLFW/lib
    SET LINKER_FLAGS= -lopengl32 -lglfw3
    

    to:

    SET COMPILER_FLAGS= -I./Dependencies/GLFW/Include -I./Dependencies/GLFW/lib
    SET LINKER_FLAGS= -L./Dependencies/GLFW/lib -lopengl32 -lglfw3
    

    and with luck this particular problem:

    cannot find -lglfw3
    

    will be solved.