54

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?

Ren
  • 801
  • 1
  • 8
  • 12

4 Answers4

110

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.

starball
  • 20,030
  • 7
  • 43
  • 238
simont
  • 68,704
  • 18
  • 117
  • 136
  • Thanks for an answer. Now I'm getting error that it doesn't find them, I did install them. `/usr/bin/ld: error: cannot find -lOpenGL /usr/bin/ld: error: cannot find -lGLUT ` – Ren Feb 27 '12 at 19:14
  • Whoops, mis-typed the `target_link_libraries` in the answer. Fixed it now, and checked that it works. :) – simont Feb 27 '12 at 19:35
  • I take it that there's no issue linking to `OpenGL` or `GLUT`, then? That kind of error suggests that `GLUT_Xi` and `GLUT_Xmu` are not installed (they weren't found by `cmake`). If you're on Ubuntu, check [this link](http://ubuntuforums.org/showthread.php?t=1703770). If not, try installing `GLUT_Xi` and `GLUT_Xmu` for your system. – simont Feb 27 '12 at 19:52
  • Thanks! That removed those.. but one more problem persists, will you able to help me this time too ? http://pastebin.com/EekEQyfs I feel hopeless. – Ren Feb 27 '12 at 19:55
  • I think that's better off in a new question; if you can post your `main.cpp` (if it's small), and your `CMakeLists.txt` I'll see if I can help. – simont Feb 27 '12 at 20:01
  • CMake : http://pastebin.com/tGyM2LAU main.cpp : http://pastebin.com/YmmPzCAX I will accept your answer, but please help me with this. – Ren Feb 27 '12 at 20:06
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/8273/discussion-between-yuki-and-simont) – Ren Feb 27 '12 at 20:20
  • Ok, I have a working `CMakeLists.txt` now. Will append to answer. – simont Feb 27 '12 at 20:22
  • When I use `find_package(OpenGL REQUIRED)`, no error occurs yet `OpenGL_FOUND` is blank string. the code & output is here: http://codepad.org/wPV253Js, why is that? – zhangxaochen Oct 10 '16 at 00:50
  • 2
    You may want to update your answer to use imported targets like `target_link_libraries(testas PRIVATE OpenGL::OpenGL GLUT::GLUT)`. And then you do not really need `include_directories()` stuff. cmake_minimum should also be updated then, but I do not really remember what would be the minimum minimum. I guess that was 3.0, with 3.6 you are surely safe. – Slava Feb 16 '18 at 11:22
  • The variable `GLUT_LIBRARY` has been deprecated even in [CMake 2.6](https://github.com/Kitware/CMake/blob/v2.4.0/Modules/FindGLUT.cmake#L103). [CMake 2.24](https://github.com/Kitware/CMake/blob/v3.24.0/Modules/FindGLUT.cmake) no longer sets this variable. Instead, the variable `GLUT_LIBRARIES` should be used. – Tsyvarev Mar 30 '23 at 10:27
20

In recent versions of CMake (3.10+) there is a new way to use OpenGL using a so-called IMPORTED target:

cmake_minimum_required(VERSION 3.10)

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

add_dependencies(testas OpenGL::OpenGL)
include_directories(${GLUT_INCLUDE_DIRS} )

target_link_libraries(testas OpenGL::OpenGL ${GLUT_LIBRARY} )

At the moment the only practical difference seems to be on Linux (where GLVND is used if available), but presumably this solution should be more future-proof, as CMake has more information about your intentions and other tools parsing your CMakeFiles will better understand the dependency tree.

minexew
  • 856
  • 9
  • 15
  • 1
    This worked for me on `macOS Mojave 10.14.3` with the GitHub project `vsthost` by `wtrsltnk` – vaid Mar 13 '19 at 14:56
  • 1
    Note that I had to use `${GLUT_LIBRARIES}` (plural) – Alexis Wilke May 15 '20 at 20:20
  • On Mac (Big Sur 11.2 in my case), one will have to fall back to `target_link_libraries( testas ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} )` because Apple ships its own OpenGL without `OpenGL::OpenGL` target. – honey_badger Feb 08 '21 at 18:22
5

As of recently you can use GLUT::GLUT:

cmake_minimum_required(VERSION 2.8)

project(testas)

find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)

add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} OpenGL::GL GLUT::GLUT)
Kada B
  • 140
  • 1
  • 11
-4

I use these two cmake files to build my OpenGL projects, and they all work well.

I only test these two cmake files under Deepin Linux. Deepin Linux is a Chinese grown Linux system like Ubuntu or from Debian.

First, the main CMakeLists.txt

cmake_minimum_required(VERSION 3.1.0)
project(project_name)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules/")

find_package(OpenGL REQUIRED)
find_package(FREEGLUT REQUIRED)
find_package(GLEW REQUIRED)

if(NOT ${OPENGL_FOUND})
    message("OPENGL not found")
endif()

include_directories(
    ${PROJECT_SOURCE_DIR}
    ${FREEGLUT_INCLUDE_DIR}
    ${GLEW_INCLUDE_DIR}
    ${OPENGL_INCLUDE_DIR}
    )

message(${OPENGL_INCLUDE_DIR})
add_executable(${PROJECT_NAME}  ${PROJECT_SOURCE_DIR}/filename.cpp) 
target_link_libraries(${PROJECT_NAME} 
${OPENGL_LIBRARY}
${FREEGLUT_LIBRARY}
${GLEW_LIBRARY}
)

Second, the find GLUT cmake file under CMakeModules directory

# Try to find the FREEGLUT library
#
# FREEGLUT_INCLUDE_DIR
# FREEGLUT_LIBRARY
# FREEGLUT_FOUND

FIND_PATH(
  FREEGLUT_INCLUDE_DIR GL/freeglut.h GL/gl.h GL/glu.h GL/glew.h
  ${CMAKE_INCLUDE_PATH}
  $ENV{include}
  ${OPENGL_INCLUDE_DIR}
  /usr/include
  /usr/local/include
)

SET(STORE_CMAKE_FIND_FRAMEWORK ${CMAKE_FIND_FRAMEWORK})
SET(CMAKE_FIND_FRAMEWORK NEVER)

FIND_LIBRARY(
  FREEGLUT_LIBRARY
  NAMES freeglut_static freeglut glut GL
  PATH
    /opt/local/lib
    ${CMAKE_LIBRARY_PATH}
    $ENV{lib}
    /usr/lib
    /usr/local/lib
)

SET(CMAKE_FIND_FRAMEWORK ${STORE_CMAKE_FIND_FRAMEWORK})

IF (FREEGLUT_INCLUDE_DIR AND FREEGLUT_LIBRARY)
   SET(FREEGLUT_FOUND TRUE)
ENDIF (FREEGLUT_INCLUDE_DIR AND FREEGLUT_LIBRARY)

IF (FREEGLUT_FOUND)
   IF (NOT FREEGLUT_FIND_QUIETLY)
      MESSAGE(STATUS "Found FREEGLUT: ${FREEGLUT_LIBRARY}")
   ENDIF (NOT FREEGLUT_FIND_QUIETLY)
ELSE (FREEGLUT_FOUND)
   IF (FREEGLUT_FIND_REQUIRED)
      MESSAGE(FATAL_ERROR "Could not find FREEGLUT")
   ENDIF (FREEGLUT_FIND_REQUIRED)
ENDIF (FREEGLUT_FOUND)
Community
  • 1
  • 1
wangzheqie
  • 53
  • 5