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

cmake freeglut
cmake opengl glfw
cmake opengl macos
could not find glut (missing: glut_glut_library glut_include_dir)
glad cmake
freeglut linux
cmake glew
the free glut project

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?

How to compile GLUT OpenGL project with CMake and Kdevelop in , 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)  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.

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.

Using GLEW, GLFW, and GLM - D'Code, GLUT has been the defacto standard library for many OpenGL programmers, but it has not been OSX/Linux users should first use cmake to compile GLFW from source. Test your GLFW installation by typing the following program: Visual Studio; Codeblock; Eclipse; Xcode; Unix make; KDevelop. I decided to create this tutorial about CMake and OpenGL since I couldn’t find many articles about this specific topic on the Internet. Most CMake tutorials focus on the very basic usage - one file main.cpp and then create project with one CMake function add_executable and that’s it.

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)

How to setup OpenGL project with CMake, Description:How to compile GLUT + OpenGL project with CMake and Kdevelop in How to cross compile for linux x86 with linux amd64,  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

CLion compile glut linux - c++ - android, What is more, this project contains the following dependencies: GLAD; GLFW3; GLM; stb_image. The one dependency that is missing is Assimp  Comment compiler GLUT + OpenGL project avec CMake et Kdevelop sous linux? comme disent les titres Je ne peux pas sembler construire le projet avec OpenGL et Glut. j'obtiens des erreurs de référence non définies pour les fonctions OpenGL.

Kdevelop cmake tutorial, I researched and tried to apply what was mentioned in a similar answer here: How to compile GLUT + OpenGL project with CMake and Kdevelop in linux? I'm trying to compile a C++ OpenGl/Glut program in KDevelop 3. When I compile I get a million glut errors that I assume occur because glut isn't linked. So, I go to: Project->Project Options->Configure Options->Linker Flags and put in -lglut, but when I run configure I get ***Exited with status: 77*** but with no other errors.

Comment compiler GLUT + OpenGL project avec CMake et , For example, KDevelop 4 supports building projects with CMake -- which does How to compile GLUT + OpenGL project with CMake and Kdevelop in linux? Compiling OpenGL (GLUT) on Linux I've just bought "OpenGL Programming Guide" 3rd ed. I'm trying to compile the very first program (see below) but can't figure out how to compile and build it.

Comments
  • 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
  • Whoops, mis-typed the target_link_libraries in the answer. Fixed it now, and checked that it works. :)
  • 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. If not, try installing GLUT_Xi and GLUT_Xmu for your system.
  • Thanks! That removed those.. but one more problem persists, will you able to help me this time too ? pastebin.com/EekEQyfs I feel hopeless.
  • 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.
  • This worked for me on macOS Mojave 10.14.3 with the GitHub project vsthost by wtrsltnk