Adding GLEW to project (CMake)
Asked Answered
V

2

7

I want to link glfw and glew to my project for graphics programming.

Adding glfw was pretty straight forward, I followed the instructions on their website. Creating a window with glfw worked perfectly.

However, I can't see what's wrong with my CMakeLists.txt for adding GLEW. The program gives the error: "GL/glew.h: No such file or directory".

My CMakeLists.txt:

cmake_minimum_required( VERSION 3.5 )

project(Starting)

find_package( OpenGL REQUIRED )

set( GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE )
set( GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE )
set( GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE )

add_subdirectory( ${PROJECT_SOURCE_DIR}/GLEW/build/cmake )
add_subdirectory( ${PROJECT_SOURCE_DIR}/GLFW )

add_executable( Starting ${PROJECT_SOURCE_DIR}/src/main.cxx )

target_link_libraries( Starting glew32s glfw )

I've tried giving it the names GLEW, glew, glew32 instead but nothing changed. The library is downloaded from here: https://github.com/Perlmint/glew-cmake

If it has any importance, this is the batch file with which I run my CMakeLists.txt (located in a build folder inside my project source directory):

@echo off

cmake -G"Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug ..

make all

Looking at OpenGL projects on github didn't help since almost all of them are using visual studio. It would be great if someone could tell me what I got wrong.

Vicennial answered 22/3, 2018 at 16:54 Comment(0)
A
6

While Julia's suggestion will likely work, there is a find script included with CMake for GLEW, assuming you are using a new enough version, so you should be using that instead of including paths manually. Just add the following:

find_package(GLEW 2.0 REQUIRED)
target_link_libraries(Starting GLEW::GLEW)

This will find GLEW on your system then both link with the necessary libraries and add the necessary include directories.

Anomalism answered 22/3, 2018 at 17:51 Comment(5)
Yes, and (in 2023 if not before) this works for both glfw and glew: find_package(glfw3 REQUIRED), find_package(GLEW REQUIRED), target_link_libraries(Project glfw GLEW::glew)Sauls
@CraigReynolds Does this work on linux? I'm having include errors doing that in Linux with vcpkg + cmakeExserviceman
@Exserviceman — sorry, I only tried this on my macOS 12.6 laptop. I'm a CMake newbie so can't offer any useful advice.Sauls
@Exserviceman It should work fine on Linux but vcpkg has it's own mechanism for integrating with CMake which you'll have to also do for it to be able to find vcpkg libraries, see this link for details: learn.microsoft.com/en-us/vcpkg/users/buildsystems/…Anomalism
Yes, that works great on Linux too. It doesn't seem to work very well in Windows though, without first setting up proper search paths.Kweilin
T
0

Your issue is you're forgetting to add the GLEW include directories to your project. You can use target_include_directories or include_directories, the only difference being where you put it in your CMakeLists.txt and the syntax.

I prefer target_include_directories so your CMakeLists.txt after adding it would look like this:

cmake_minimum_required( VERSION 3.5 )

project(Starting)

find_package( OpenGL REQUIRED )

set( GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE )
set( GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE )
set( GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE )

add_subdirectory( ${PROJECT_SOURCE_DIR}/GLEW/build/cmake )
add_subdirectory( ${PROJECT_SOURCE_DIR}/GLFW )

add_executable( Starting ${PROJECT_SOURCE_DIR}/src/main.cxx )
target_include_directories(Starting PRIVATE
    ${PROJECT_SOURCE_DIR}/GLEW/include
)

target_link_libraries( Starting glew32s glfw )
Toscano answered 22/3, 2018 at 17:9 Comment(1)
@AlexandruIca No problem. Please mark my answer as correct if it helped.Toscano

© 2022 - 2024 — McMap. All rights reserved.