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
Meaning of Tilde in Linux Bash (Not Home Directory)
Need to Remove the Count from the Output When Using "Uniq -C" Command
Bash Script to Remove 'X' Amount of Characters the End of Multiple Filenames in a Directory
How to Export a Variable in Bash
Node.Js: Cannot Find Module 'Request'
How to Get the Percentage of Memory Free with a Linux Command
How Do Applications Resolve to Different Versions of Shared Libraries at Run Time
Converting Jiffies to Milli Seconds
How to Attach a File Using Mail Command on Linux
Bash Capturing Output of Awk into Array
Why Do We Need a Bootloader in an Embedded Device
Sort Not Sorting as Expected (Space and Locale)
File Names with Spaces in Bash
X86_64 Assembly Linux System Call Confusion
How to Add a Line of Text to the Middle of a File Using Bash