wr0mb3l
wr0mb3l

Reputation: 13

CMake not finding GL/glew.h

I'm using CLion under pop_OS! 20.04 and am currently trying to get CMake to work with GLEW because I want to follow these tutorials to get started with OpenGL. I'm relatively new to C/C++ and completely new to CMake.

I installed the libgl1-mesa-dev,libglew-dev,libglfw3,libglfw3-dev packages with apt-get install and the glew.h is located alongside the other GL header files in /usr/include/GL.

When trying to compile a simple program:

#include <GL/glew.h>

int main() {
    return 0;
}

cmake can't find the headerfile:

test/main.cpp:1:10: fatal error: GL/glew.h: No such file or directory
    1 | #include <GL/glew.h>
      |          ^~~~~~~~~~~

Do I have to manually add these header files in CMakeLists.txt for cmake to find them? I tried like a dozen different suggestions but I didn't get it to work. For example, I tried

cmake_minimum_required(VERSION 3.17)
project(test)

set(CMAKE_CXX_STANDARD 14)

find_package(GLEW REQUIRED)
include_directories(${GLEW_INCLUDE_DIRS})
link_libraries(${GLEW_LIBRARIES})

target_link_libraries(test GLEW::GLEW)

but this results in

CMake Error at /app/extra/clion/bin/cmake/linux/share/cmake-3.17/Modules/FindPackageHandleStandardArgs.cmake:164 (message):
  Could NOT find GLEW (missing: GLEW_INCLUDE_DIRS GLEW_LIBRARIES)

Is this somehow a problem with CLion and I need to include libraries into my project in a different manner? Or am I using cmake in a wrong way?

Upvotes: 1

Views: 3673

Answers (2)

zkoza
zkoza

Reputation: 2869

The answer by @vre is OK. I only add how to find it yourself.

First, run

cmake --help-module-list

and look for GLEW. Under Linux, you can simplify this step to

cmake --help-module-list | grep -i Glew

where -i stands for "case insesitive match", as we usually don't remember if it's GLEW or perhaps Glew. Once you know the module name, you can ask for the specific help related to just this module:

cmake --help-module FindGLEW

The answer is there. Read and enjoy!

PS. There's no need to memorize this answer. All can be reduced to cmake --help.

Upvotes: 2

vre
vre

Reputation: 6744

Your last try was nearly right.

Forget about include_directories and link_libraries commands. All it needs when doing modern CMake is an add_executable(test main.cpp) followed by a target_link_libraries(test PRIVATE GLEW::glew) command. The target GLEW::glew (case matters here) bundles all compile definitions, include directories, libraries, and further settings that are needed to compile and link your example.

Your CMakeLists.txt would now look like this:

cmake_minimum_required(VERSION 3.17)
project(test)

set(CMAKE_CXX_STANDARD 14)

find_package(GLEW REQUIRED)

add_executable(myTest main.cpp)

target_link_libraries(myTest PRIVATE GLEW::glew)

Edit: Just noticed, that GLEW::GLEW can also be used.

Upvotes: 2

Related Questions