Home > Enterprise >  CLion Not Finding GLUT with CMake
CLion Not Finding GLUT with CMake

Time:01-17

I have a problem that I can't seem to find the settings to modify.

When attempting to find the GLUT package using CLion's CMake utilities on Ubuntu, it does not find GLUT. Using command-line CMake and Makefile commands, however, finds the dependencies perfectly and allows the following to generate and compile:

# CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(mre)

set(CMAKE_CXX_STANDARD 20)

find_package(OpenGL REQUIRED) # Works in CLion and terminal
find_package(GLUT REQUIRED) # Works only in terminal
include_directories(GL)

add_executable(mre mre.cpp)
target_link_libraries(mre -lglut -lGLU -lGL)
// mre.cpp
#include <GL/gl.h>
#include <GL/glut.h>

int main()
{
    return 0;
}

Whereas attempting to use these files in a CLion project would cause errors (first unable to find GLUT, mitigated by manually setting library and include variables; then GL/glut.h: No such file or directory, which I am unable to fix).

Does anyone have any suggestions? I'm assuming it's something to do with a working directory or prefixes, but CMAKE_PREFIX_PATH is unset in CLion, and setting it to various values does nothing to solve the problem.

Thanks!

CodePudding user response:

You know something has gone wrong when you write include_directories or -l flags by hand. You should absolutely always link to libraries via their imported targets.

See the documentation:

Try this revision:

cmake_minimum_required(VERSION 3.16)
project(mre)

find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)

add_executable(mre mre.cpp)
target_link_libraries(mre PRIVATE OpenGL::GL OpenGL::GLU GLUT::GLUT)
target_compile_features(mre PRIVATE cxx_std_20)

As for not being able to find GLUT... just set CMAKE_PREFIX_PATH in CLion's settings to whichever directory on your system contains include/GL/glut.h.

CodePudding user response:

Alternative solution

CLion was installed through the Software Center via Flatpak, which uses some kind of filesystem sandboxing that may be interfering with paths. I tried explicitly allowing /usr and related paths, but had no effect.

I have reinstalled via JetBrains's official archive, which correctly detects GLUT and OpenGL. Their official snap also works properly.

  •  Tags:  
  • Related