I am trying to learn how to make games and simulations in c using glad and glfw. This error occurred when trying to use the struct Window
in any function as parameter or just to declare a Window
instance. I am getting the 'Window': undeclared identifier
error. I understood that this may mean that I have a circular include (which I can't seem to figure out where) by researching the error on stackoverflow. (I am quite new to c, so any help I gladly appreciated)
Core.h:
#ifndef MINECRAFTCLONE_CORE_H
#define MINECRAFTCLONE_CORE_H
#include <stdio.h>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
extern int error(char* error);
#endif
Core.c:
#include "Core.h"
int error(char* error)
{
printf("%s", error);
return -1;
}
Window.h:
#ifndef CORE_WINDOW_H
#define CORE_WINDOW_H
#include "Core.h"
struct Window
{
int width;
int height;
char* title;
GLFWwindow* res;
};
extern int coreCreateWindow(struct Window* window, int width, int height, char* title);
extern int coreLoopWindow(struct Window* window);
#endif
Window.c:
#include "Core.h"
#include "Window.h"
int coreCreateWindow(struct Window* window, int width, int height, char* title)
{
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
if (!glfwInit())
return error((char*)"Failed to initialize glfw");
window->width = width;
window->height = height;
window->title = title;
window->res = glfwCreateWindow(window->width, window->height, window->title, 0, 0);
if (!window->res)
return error((char*)"Failed to create glfw window");
glfwMakeContextCurrent(window->res);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
return error((char*)"Failed to initialize glad");
return 0;
}
int coreLoopWindow(struct Window* window)
{
while (!glfwWindowShouldClose(window->res))
{
glfwPollEvents();
}
glfwDestroyWindow(window->res);
glfwTerminate();
return 0;
}
main.c:
#include "Core.h"
#include "Window.h"
int main()
{
Window* window;
return 0;
}
CodePudding user response:
You have not defined a type called Window
with which you could define a variable like
Window *window;
You have defined a struct Window
with which you define your window
variable in main()
like
struct Window *window;
in the same way you are already defining all your function declaration's window
parameters.