How to Compile Glut + Opengl Project with Cmake and Kdevelop in Linux

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)

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.

CLion compile glut linux

target_link_libraries wants a target name. Targets are specified by (among others) add_executable, add_library and add_custom_target.

In other words, target_link_libraries(testas ...) should work.
While you're at it, you should consider switching your include_directories(...) to target_include_directories(testas ...) as well.

Opengl Superbible Configuring an IDE

As you noted, the configuration is in CMakeLists.txt moreso than KDevelop, which is told what to do by the CMake manager. That said, google turned up a few points of interest regarding opengl config with CMake. These look like they have the main points of interest:

http://www.cmake.org/pipermail/cmake/2009-February/027234.html

http://www.cmake.org/pipermail/cmake/2009-February/027330.html

Basically just:

find_package(OpenGL)
INCLUDE_DIRECTORIES(${OPENGL_INCLUDE_DIR})
target_link_libraries(targetname ${OPENGL_LIBRARIES})

How to build the Qt + cmake + QGLWidget

In Linux (Arch Linux) GLUT must be linked (use this answer as a base):

cmake_minimum_required(VERSION 3.5)

project(untitled2 LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_executable(untitled2
main.cpp
)

find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
find_package(Qt5 COMPONENTS Widgets OpenGL REQUIRED)

target_link_libraries(untitled2 PRIVATE Qt5::Widgets Qt5::OpenGL ${OPENGL_LIBRARIES} ${GLUT_LIBRARY})

Also when you include system or third-party library you must use <> instead of "" so you must change to:

#include <QApplication>
#include <QGLWidget>
#include <QDebug>
#include <cmath>
// ...

How to link to FreeGlut instead of Glut on macOS using cmake and find_package?

So FindGLUT is a builtin module
ref: https://cmake.org/cmake/help/latest/module/FindGLUT.html

looking at the source code:
see https://github.com/Kitware/CMake/blob/master/Modules/FindGLUT.cmake

so it seems to use find_path()

find_path(GLUT_INCLUDE_DIR glut.h ${OPENGL_LIBRARY_DIR})

You can try to set GLUT_ROOT or OPENGL_LIBRARY_DIR as default path to help CMake to find the correct one...
ref https://cmake.org/cmake/help/latest/command/find_path.html



Related Topics



Leave a reply



Submit