How to compile GLUT + OpenGL project with CMake and Kdevelop in linux?

find_package(OpenGL) will find the package for you, but it doesn’t link the package to the target.

To link to a library, you can use target_link_libraries(<target> <item>). In addition, you also need to set the include directory, so that the linker knows where to look for things. This is done with the include_directories.

An example CMakeLists.txt which would do this looks something like this:

cmake_minimum_required(VERSION 2.8)

add_executable(testas main.cpp)
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
include_directories( ${OPENGL_INCLUDE_DIRS}  ${GLUT_INCLUDE_DIRS} )

target_link_libraries(testas ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} )

If OpenGL is a necessity for your project, you might consider either testing OpenGL_FOUND after the find_package(OpenGL) or using REQUIRED, which will stop cmake if OpenGL is not found.

For more information and better examples:

  • CMake documentation, target_link_libraries
  • CMake documentation, find_package
  • CMake wiki: how to find libraries
  • Forum post with solution: cmake and opengl
  • Tutorial for CMake by

In particular, the CMake wiki and cmake and opengl links should give you enough to get things working.

Leave a Comment