如何解决使用GLFW和GLAD制作C ++程序OpenGL时遇到这些错误
尝试遵循本教程时(我第一次制作OpenGL程序),我得到这些错误。 有人知道为什么会发生吗?我知道链接器在链接时遇到麻烦,但是我不知道为什么。还有优化/改进代码以及改善问题的技巧吗?我对stackoverflow很不好笑
编辑1:我现在知道链接器缺少共享库,但是我没有要链接的链接/我的图书馆GLAD / GLFW中没有链接的链接。
编辑2:经过更多研究,事实证明某些事情可能没有正确定义。但是这些错误来自GLAD / GLFW,而不是我的代码。有什么帮助吗? (Source)
代码:
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
void framebuffer_size_callback(GLFWwindow* window,int width,int height);
void processInput(GLFWwindow *window);
// settings
const unsigned int SCR_WIDTH = 854;
const unsigned int SCR_HEIGHT = 480;
int main()
{
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MInor,3);
glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT,GL_TRUE);
#endif
// glfw window creation
// --------------------
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH,SCR_HEIGHT,"epic new window",NULL,NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window,framebuffer_size_callback);
// glad: load all OpenGL function pointers
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// render loop
// -----------
while (!glfwWindowShouldClose(window))
{
// input
// -----
processInput(window);
// render
// ------
glClearColor(0.0f,0.0f,0.3f,1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// glfw: swap buffers and poll IO events (keys pressed/released,mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
// glfw: terminate,clearing all prevIoUsly allocated GLFW resources.
// ------------------------------------------------------------------
glfwTerminate();
return 0;
}
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow *window)
{
if(glfwGetKey(window,GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetwindowShouldClose(window,true);
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window,int height)
{
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
glViewport(0,width,height);
}
错误:
g++ -Wall -o "main" "main.cpp" (in directory: /home/leonard/Desktop/stuff/Mega Royale/src)
/usr/bin/ld: /tmp/ccBn024k.o: in function `main':
main.cpp:(.text+0xd): undefined reference to `glfwInit'
/usr/bin/ld: main.cpp:(.text+0x1c): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.cpp:(.text+0x2b): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.cpp:(.text+0x3a): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.cpp:(.text+0x5b): undefined reference to `glfwCreateWindow'
/usr/bin/ld: main.cpp:(.text+0x93): undefined reference to `glfwTerminate'
/usr/bin/ld: main.cpp:(.text+0xa9): undefined reference to `glfwMakeContextCurrent'
/usr/bin/ld: main.cpp:(.text+0xbc): undefined reference to `glfwSetFramebufferSizeCallback'
/usr/bin/ld: main.cpp:(.text+0xc3): undefined reference to `glfwGetProcAddress'
/usr/bin/ld: main.cpp:(.text+0xcb): undefined reference to `gladLoadGLLoader'
/usr/bin/ld: main.cpp:(.text+0x10f): undefined reference to `glfwWindowShouldClose'
/usr/bin/ld: main.cpp:(.text+0x12b): undefined reference to `glad_glClearColor'
/usr/bin/ld: main.cpp:(.text+0x14c): undefined reference to `glad_glClear'
/usr/bin/ld: main.cpp:(.text+0x15f): undefined reference to `glfwSwapBuffers'
/usr/bin/ld: main.cpp:(.text+0x164): undefined reference to `glfwPollEvents'
/usr/bin/ld: main.cpp:(.text+0x16b): undefined reference to `glfwTerminate'
/usr/bin/ld: /tmp/ccBn024k.o: in function `processInput(GLFWwindow*)':
main.cpp:(.text+0x193): undefined reference to `glfwGetKey'
/usr/bin/ld: main.cpp:(.text+0x1ae): undefined reference to `glfwSetwindowShouldClose'
/usr/bin/ld: /tmp/ccBn024k.o: in function `framebuffer_size_callback(GLFWwindow*,int,int)':
main.cpp:(.text+0x1ce): undefined reference to `glad_glViewport'
collect2: error: ld returned 1 exit status
Compilation Failed.
教程: LearnOpenGL
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。