Setting Up OpenGL with GLFW, GLAD, and CMake

Integrating GLFW with CMake

To incorporate GLFW into your project:

  1. Download the GLFW source code and place it in a third_party directory.
  2. Add the following configuration to your main CMakeLists.txt file, adapted from GLFW's official documentation:
cmake_minimum_required(VERSION 3.14)
project(opengl_project)

find_package(OpenGL REQUIRED)

set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)

add_subdirectory(third_party/glfw-3.3.8)

add_executable(opengl_app main.cpp)

target_link_libraries(opengl_app OpenGL::GL)
target_link_libraries(opengl_app glfw)

Configuring GLAD with CMake

GLAD serves as an OpenGL loading library. For this setup, we'll use OpenGL 3.3. Follow these steps:

  1. Download the GLAD include and src directories to third_party/glad/
  2. Add the following to your main CMakeLists.txt file:
add_subdirectory(third_party/glad)
target_link_libraries(opengl_app glad)

Create a CMakeLists.txt file in the third_party/glad/ directory with the following content:

cmake_minimum_required(VERSION 3.0)
project(Glad)

add_library(glad include/glad/glad.h src/glad.c)
target_include_directories(glad PUBLIC include/)

Basic OpenGL Application

Here's a simple OpenGL application that creates a window and clears it with a specific color:

#include 
#include 

#include <iostream>

void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
    glViewport(0, 0, width, height);
}

int main() {
    // Initialize GLFW
    if (!glfwInit()) {
        std::cerr << "Failed to initialize GLFW" << std::endl;
        return -1;
    }

    // Configure GLFW
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    // Create a GLFW window
    GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL Window", NULL, NULL);
    if (!window) {
        std::cerr << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(window);
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

    // Initialize GLAD
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
        std::cerr << "Failed to initialize GLAD" << std::endl;
        return -1;
    }

    // Render loop
    while (!glfwWindowShouldClose(window)) {
        // Clear the screen with a blue color
        glClearColor(0.2f, 0.3f, 0.8f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        // Swap buffers and poll IO events
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    // Clean up resources
    glfwTerminate();
    return 0;
}

Building with CMake

To compile your project, use these CMake commands:

cmake -S . -B build
cmake --build build

Tags: OpenGL GLFW GLAD CMake graphics

Posted on Fri, 10 Jul 2026 17:01:16 +0000 by cunoodle2