我一直在尝试使用高兴,但是当我使用它时会遇到各种问题。首先,我只包含它就没有错误
#include <glad/glad.h>
#include <GLFW/glfw3.h>
我只有在使用它时才开始出现问题
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initalize Glad" << std::endl;
return -1;
}
然后我得到错误:
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
之后,我尝试了包括以下内容的切换位置:
#include <GLFW/glfw3.h>
#include <glad/glad.h>
这给了我同样的错误。
#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>
这给了我:
error: OpenGL header already included, remove this include, glad already provides it
所以我尝试使用#define GLFW_INCLUDE_NONE
,但什么也没做。我是否可能缺少某些gladInit
功能或其他东西?
这是完整的代码示例:
#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;
}