Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 327
Ren Avatar asked Feb 27 '12 04:02

Ren


1 Answers

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:

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

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

like image 107
simont Avatar answered Oct 16 '22 03:10

simont