Linking GLEW with CMake

2019-01-24 04:02发布

问题:

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

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().

回答1:

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)
if (GLEW_FOUND)
    include_directories(${GLEW_INCLUDE_DIRS})
    link_libraries(${GLEW_LIBRARIES})
endif()


回答2:

The secret of find_package(GLEW) is in FindGLEW.cmake file with cmake install.

find_path(GLEW_INCLUDE_DIR GL/glew.h)
find_library(GLEW_LIBRARY NAMES GLEW glew32 glew glew32s PATH_SUFFIXES lib64)

The find_path and find_library commands find paths in standard system paths. If you want them to find paths in user defined directories, you should tell them. For example:

set(CMAKE_PREFIX_PATH "d:/libs/glew-1.10.0")
set(CMAKE_LIBRARY_PATH "d:/libs/glew-1.10.0/lib/Release/Win32/")
find_package(GLEW REQUIRED)

Reference:

  • http://www.cmake.org/cmake/help/v3.0/command/find_path.html
  • http://www.cmake.org/cmake/help/v3.0/command/find_library.html


回答3:

I was struggling hard to link glew to cmake through command line on mac. This might be helpful but I am not sure :) I will walk you through step by step of what I have done.

I installed Cmake source from the web.

Then I went inside the cmake folder in terminal and typed

./bootstrap && make && make install 

(this will install cmake command line tools on our OS platform)

I have some exercise files. I want cmake to generate xcode files for me for all those exercise files (ex. triangles.cpp, shader.cpp etc) So i made a directory inside exercise files folder.

$ mkdir xcode 
$ cd xcode
$ cmake -G "Xcode" ..

At this point, Cmake suppose to install all xcode files that included correct libraries. But there was an error :

$ cmake -G "Xcode" ..
CMake Warning (dev) at CMakeLists.txt:3 (cmake_minimum_required):
Compatibility with CMake < 2.4 is not supported by CMake >= 3.0.
This warning is for project developers.  Use -Wno-dev to suppress it.

system name is: Darwin-14.1.0
system processor is: x86_64
-- Could NOT find GLEW (missing:  GLEW_INCLUDE_DIR GLEW_LIBRARY) 
-- Could NOT find Doxygen (missing:  DOXYGEN_EXECUTABLE) 
-- Using Cocoa for window creation
-- Using NSGL for context creation
-- Building GLFW only for the native architecture

CMake Error: The following variables are used in this project, but they are  set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
GLEW_LIBRARY
linked by target "TextureLoader" in directory /Users/Mydir/Desktop/Exercise/Exercise Files

-- Configuring incomplete, errors occurred!

Then to make sure I have installed GLEW and all its libraries correctly, I ran

$brew install glew 

Yes, I have installed glew already but it was NOT linked. See the Warning below:

Warning: glew-1.12.0 already installed, it's just not linked

Then I ran the following commands:

$ brew unlink glew 
$ brew link glew

And I have solved the error. So just make sure that you have linked glew. Hope this helps.

Happy Coding :)