学习OpenGL时找不到glClear
Posted
技术标签:
【中文标题】学习OpenGL时找不到glClear【英文标题】:Cannot find glClear while learning OpenGL 【发布时间】:2017-02-28 01:34:09 【问题描述】:我正在尝试学习 OpenGL。我使用 Clion 作为 IDE,它使用 CMakeLists.txt 文件来组织/编译项目。
编译器由于某种原因找不到glClear
:
Undefined symbols for architecture x86_64:
"_glClear", referenced from:
_main in main.cpp.o
"_glClearColor", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [GraphicsPractice] Error 1
make[1]: *** [CMakeFiles/GraphicsPractice.dir/all] Error 2
make: *** [all] Error 2
我能够成功链接GLEW
和GLFW
,但是,当我调用glClear(GL_COLOR_BUFFER_BIT)
时,我的代码会中断。我不确定为什么。如果有人能帮助我指出正确的方向,那就太好了。
CMakeList.txt
cmake_minimum_required(VERSION 3.6)
project(GraphicsPractice)
set(CMAKE_CXX_STANDARD 14)
set(SOURCE_FILES src/main.cpp)
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
add_subdirectory(deps/glfw)
find_package(OpenGl REQUIRED)
find_package(GLEW REQUIRED)
include_directories("deps/glfw/include/")
add_executable($PROJECT_NAME $SOURCE_FILES)
target_link_libraries($PROJECT_NAME glfw glew)
main.cpp
#define GLEW_STATIC
#include <GL/glew.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
int main()
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL", 0, nullptr);
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
glewInit();
while (!glfwWindowShouldClose(window))
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
glfwSwapBuffers(window);
glfwPollEvents();
glfwTerminate();
return 0;
【问题讨论】:
这是一个链接器错误,而不是编译器错误。您没有链接到 opengl 库。 你在使用OSX吗? @genpfault 是的,我是。 【参考方案1】:您需要链接 OpenGL 库。即:
target_link_libraries($PROJECT_NAME $OPENGL_LIBRARIES glfw glew etc...)
【讨论】:
以上是关于学习OpenGL时找不到glClear的主要内容,如果未能解决你的问题,请参考以下文章