I've been trying to use glad, however I get all sorts of issues when I use it. First off, I get no errors by just including it
#include <glad/glad.h>
#include <GLFW/glfw3.h>
I only start to get problems when I use it
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initalize Glad" << std::endl;
return -1;
}
Then I get the error:
Undefined symbols for architecture x86_64:
"_gladLoadGLLoader", referenced from:
_main in main-479587.o
"_glad_glClear", referenced from:
_main in main-479587.o
"_glad_glClearColor", referenced from:
_main in main-479587.o
"_glad_glViewport", referenced from:
_main in main-479587.o
ld: symbol(s) not found for architecture x86_64
After that I tried switch place of the includes:
#include <GLFW/glfw3.h>
#include <glad/glad.h>
This gives me the same error.
#ifdef __APPLE__
/* Defined before OpenGL and GLUT includes to avoid deprecation messages */
#define GL_SILENCE_DEPRECATION
#include <OpenGL/gl.h>
#include <GLUT/glut.h>
#else
#include <GL/gl.h>
#include <GL/glut.h>
#endif
#include <GLFW/glfw3.h>
#include <glad/glad.h>
This gives me:
error: OpenGL header already included, remove this include, glad already provides it
So I tried using #define GLFW_INCLUDE_NONE
, but that did nothing. Was I maybe missing some gladInit
function or something?
Here's the full code example:
#ifdef __APPLE__
/* Defined before OpenGL and GLUT includes to avoid deprecation messages */
#define GL_SILENCE_DEPRECATION
#include <OpenGL/gl.h>
#include <GLUT/glut.h>
#else
#include <GL/gl.h>
#include <GL/glut.h>
#endif
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#include <glad/glad.h>
#include <iostream>
int main(void)
{
if (!glfwInit())
{
std::cout << "Failed to initalize GLFW" << std::endl;
return -1;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
const int windowWidth = 1280;
const int windowHeight = 720;
const char* windowTitle = "Game";
GLFWwindow* window = glfwCreateWindow(
windowWidth,
windowHeight,
windowTitle,
nullptr,
nullptr
);
if (window == nullptr)
{
std::cout << "Failed to create GLFW window." << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initalize Glad" << std::endl;
return -1;
}
glViewport(0, 0, windowWidth, windowHeight);
while (!glfwWindowShouldClose(window))
{
glClearColor(250 / 255, 119 / 255, 110 / 255, 1);
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}