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

cmakelinuxopengl

As the titles says I can't seem to build the project with OpenGL and Glut.

I get Undefined reference errors for OpenGL functions.

I tried doing :

project(testas)
find_package(OpenGL)
find_package(GLUT)
add_executable(testas main.cpp)

But that doesn't work.

Any suggestions?

Best Solution

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)

project(testas)
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:

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

Related Question