学习OpenGL时找不到glClear

时间:2017-02-28 01:34:10

标签: c++ opengl glfw clion glew

我正在尝试学习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

我能够成功链接GLEWGLFW,但是,当我调用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;
}

1 个答案:

答案 0 :(得分:2)

您需要链接OpenGL库。即:

target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES} glfw glew etc...)