Search code examples
c++openglglfwclionglew

Cannot find glClear while learning OpenGL


I'm attempting to learn OpenGL. I'm using Clion as an IDE, which uses a CMakeLists.txt file to organize/compile the project.

The compiler cannot find glClear for some reason:

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

I'm able to successfully link GLEW and GLFW, however, my code breaks when I call glClear(GL_COLOR_BUFFER_BIT). I'm not sure why. If anyone could help point me in the right direction, that would be great.

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;
}

Solution

  • You need to link the OpenGL Libraries. Namely:

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