Search code examples
c++cmakeglew

Cannot open source file "GL/glew.h" when using CMake find_package()


as stated in the title, I am trying to add glew.h to my project built with CMake on Visual Studio 2019 (Windows 10), and in doing so am experiencing an error thrown during the building stage fatal error C1083: Cannot open include file: 'GL/glew.h': No such file or directory.

I am including it in engine.hpp as such #include <GL/glew.h>

CMakeLists.txt

cmake_minimum_required (VERSION 3.8)

set(PROJECT GUI_Engine)

add_executable (${PROJECT} "./src/driver/main.cpp")

set(HEADER_FILES ./src/include)
set(DEC_FILES ./src/lib)
set(PKGPATH C:/dev/vcpkg/packages)

add_library(engine ${DEC_FILES}/engine.cpp ${HEADER_FILES}/engine.hpp)

list(APPEND CMAKE_PREFIX_PATH ${PKGPATH}/sdl2_x64-windows)
find_package(SDL2 CONFIG REQUIRED)
target_link_libraries(${PROJECT} PRIVATE engine SDL2::SDL2main)
target_link_libraries(engine PRIVATE SDL2::SDL2)

list(APPEND CMAKE_PREFIX_PATH ${PKGPATH}/glew_x64-windows)
find_package(GLEW REQUIRED)
target_link_libraries(engine PRIVATE GLEW::GLEW)

Any help would be appreciated, and I apologize for my inexperience, thanks!


Solution

  • If you use vcpkg, you should just use vcpkg's CMake integration and let it handle details. Your CMakeLists.txt should not contain any absolute path, it's supposed to be a file you share with other people alongside your source.

    That being said, the immediate issue at hand comes probably from this problem:

    1. target_link_libraries(${PROJECT} PRIVATE engine SDL2::SDL2main)

      From which I guess your executable #includes some header from engine at some point. Probably engine.hpp, right?

    2. target_link_libraries(engine PRIVATE GLEW::GLEW)

      Makes Glew available to engine but privately. That is: only when building engine itself, not when building anything else (in particular, not when building the main executable).

    From point 1., the dependency on Glew is exposed in the API of engine, so it is a public dependency. So you should mark it as PUBLIC in 2.:

    target_link_libraries(engine PUBLIC GLEW::GLEW)
    

    Once CMake knows the dependency is exposed, it will also make Glew available to anything that uses engine.