Linking GLEW with CMake

Lord Zsolt picture Lord Zsolt · Dec 14, 2014 · Viewed 44.4k times · Source

How can you link GLEW to a project with CMake?

We've been trying to link GLEW to our project using CMake for at least 3 hours without any success so any help is accepted.

I'm using the FindGLEW.cmake which comes with CMake 3.1.0

CMakeLists.txt

find_package(GLEW REQUIRED)
if (GLEW_FOUND)
    include_directories($(GLEW_INCLUDE_DIRS))
endif()

Environment Variables

enter image description here enter image description here

I'm using MinGW w64 to compile the sources and we successfully linked GLFW and GLM just by copying the includes and libs to their respective folders, but after doing the same with GLEW, CMake still couldn't find it.

Sorry if I wasn't clear enough while formulating the question. I will provide any additional information required.


Edit: I've managed to link the header files by specifying their location in the CMake Cache file, though I'm getting undefined reference to glew functions like glewInit().

Answer

Jonny D picture Jonny D · Dec 15, 2014

Typical CMake scripts like FindGLEW will define variables that specify the paths and files that your project needs. If the script can't automatically identify the correct paths (usually because of nonstandard install location, which is fine), then it leaves these variables up to you to fill in.

With command line CMake, you use the -D flag to define and set the value of a given variable. Other CMake interfaces, like CMake-gui or an IDE integration, give you this ability some other way.

However you do it, you can also modify the cache directly (CMakeCache.txt) and see what CMake is using in there or just clear the cache altogether. You'll have to rerun CMake for it to pick up your changes.

When it comes to linking, that's when you need to tell CMake which libs to link. Use the link_libraries command with what the automated script gives you.

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