Skip to content

Instantly share code, notes, and snippets.

@vassvik
Last active June 15, 2024 04:15
Show Gist options
  • Save vassvik/17dc05abf488efac81300a77a13ed0f7 to your computer and use it in GitHub Desktop.
Save vassvik/17dc05abf488efac81300a77a13ed0f7 to your computer and use it in GitHub Desktop.
GLFW + glad on Windows

How to get GLFW + glad working in Windows using the Visual Studio compiler (MSVC):

From an x64 dev command prompt. I'm using VS 2019, but earlier and later versions should work the same. GLFW depends on git and cmake being available in PATH. No other dependencies.

Compiling GLFW:

git clone https://github.com/glfw/glfw

cd glfw 

mkdir build

cd build 

cmake -G "NMake Makefiles" .. -DGLFW_BUILD_EXAMPLES=OFF -DGLFW_BUILD_TESTS=OFF -DGLFW_BUILD_DOCS=OFF -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded -DCMAKE_BUILD_TYPE=RELEASE

cd ..

cmake --build build --config Release

copy glfw\build\src\glfw3.lib to your lib folder copy glfw\include\GLFW to your include folder

Getting a GL loader:

Go to https://glad.dav1d.de/, select your options.

E.g. https://glad.dav1d.de/#language=c&specification=gl&api=gl%3D4.6&api=gles1%3Dnone&api=gles2%3Dnone&api=glsc2%3Dnone&profile=core&loader=on&localfiles=on for OpenGL 4.6 core with an explicit loader included and no extensions.

Copy the two .h files and the one .c file into your include/glad folder (for simplicity for what comes later, you can put the glad.c file in your src directory or whatever)

Write your test GLFW app.

E.g. like the following simplified from the GLFW docs:

#include <glad/glad.h>
#include <glad/glad.c> // Alternatively, add glad.c to your src directly and add to the command line prompt when compiling
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
 
#include <stdlib.h>
#include <stdio.h>
 
static void error_callback(int error, const char* description)
{
    fprintf(stderr, "Error: %s\n", description);
}
 
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
    if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
        glfwSetWindowShouldClose(window, GLFW_TRUE);
}
 
int main(void)
{
    GLFWwindow* window;
 
    glfwSetErrorCallback(error_callback);
 
    if (!glfwInit())
        exit(EXIT_FAILURE);
 
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
 
    window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        exit(EXIT_FAILURE);
    }
 
    glfwSetKeyCallback(window, key_callback);
 
    glfwMakeContextCurrent(window);
    gladLoadGL(); // gladLoadGL(glfwGetProcAddress) if glad generated without a loader
    glfwSwapInterval(1);
 
    glClearColor(1.0, 0.0, 0.0, 1.0); 
    while (!glfwWindowShouldClose(window))
    {
        glfwPollEvents();

        glClear(GL_COLOR_BUFFER_BIT);
 
        glfwSwapBuffers(window);
    }
 
    glfwDestroyWindow(window);
 
    glfwTerminate();
    exit(EXIT_SUCCESS);
}

Compile the test app

cl /nologo test_glfw.c /Iinclude lib/glfw3.lib user32.lib gdi32.lib shell32.lib

This should give you a little exciting red window image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment