1

I've been trying to set up a build environment for OpenGL using glfw3 and GLAD. I'm currently using WSL2 Ubuntu with an X Server for compilation and a makefile.

However, when I run my make I receive the following error:

src/glad.c:25:10: fatal error: glad/glad.h: No such file or directory 25 | #include <glad/glad.h>

This is odd to me because it seems that the makefile is able to compile the main.cpp file and create a main.o despite also including "glad/glad.h"

File structure:

-HelloTriangle
--include
---glad
----glad.h
---KHR
----khrplatform.h
--src
---glad.c
---main.cpp
--makefile

This is my make file:

BASE_OBJS = main.o glad.o

SRC_PATH = src

OBJS = $(addprefix $(SRC_PATH)/, $(BASE_OBJS))
CXX = g++
CXXFLAGS = -g -Iinclude
LDFLAGS =
LDLIBS = -lglfw -lGL -lX11 -lpthread -lXrandr -lXi -ldl

HelloTriangle: $(OBJS)
    $(CXX) -o $@ $(LDFLAGS) $^ $(LDLIBS)    

clean:
    rm $(OBJS)

This is my main.cpp:

#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>

#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600

/*
Function to handle window resizing
*/
void framebuffer_size_callback(GLFWwindow* window, int width, int height);

int main() {
    /* 
    Initialize GLFW
    Sets version to Core profile 3.3
     */
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    /* 
    Initialize a window context for OpenGL
    Defines the windows width, height, and title
     */
    GLFWwindow* window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Hello Triangle", NULL, NULL);
    if(window == NULL) {
        std::cout << "Failed to create GLFW window" <<std::endl;
        glfwTerminate();
        return -1;
    }

    /* 
    Initialize GLAD
    Handles OS-specific function pointers
    */
    if(!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return -1;
    }

    /*
    Handle window resizing
    */
    glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

    /*
    Render loop
    */
    while(!glfwWindowShouldClose(window)) {
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

void framebuffer_size_callback(GLFWwindow* window, int width, int height) { glViewport(0, 0, width, height); }
  • Note C and C++ are different languages. You're a lot more likely to get weird results compiling C++ code with a C compiler than C code with a C++ compiler, but it's something to watch out for. – user4581301 May 25 '21 at 22:47
  • Wait a sec. My comment above may be BS. I see a rule for linking the objects together, but where are the code-to-object rules? – user4581301 May 25 '21 at 22:49
  • I'm relying on the GNU default pattern rules for code-to-object rules(found here: https://www.gnu.org/software/make/manual/html_node/Catalogue-of-Rules.html) – Ethan Klein May 25 '21 at 22:55
  • 1
    Though reading through it I think it might be that because its a c file instead of c++ it isn't using the default rule I was expecting. – Ethan Klein May 25 '21 at 22:56
  • You have no explicit rule to compile the `C` file so *make* is using *implicit rules* and those get their flags from the environment variables. In the case of `C` you need `CPPFLAGS` to be set. – Galik May 25 '21 at 23:18

1 Answers1

4

You seem to set the CXXFLAGS (for the C++ compiler), but your glad.c is compiled with the C-compiler (which checks CFLAGS)

okaestne
  • 66
  • 2