Home > front end >  modern cmake way to add glew and other opengl stuff
modern cmake way to add glew and other opengl stuff

Time:09-03

I'm create new edu project. Here is the project structure:

├── 3rd-party
│   ├── glew
│   ├── glfw
│   ├── CMakeList.txt
├── src
│   ├── main.cpp
├── CMakeList.txt

the 3rd-party Cmakelist looks like this:

# GLFW
add_subdirectory(glfw)

# GLEW
find_package(GLEW REQUIRED)

I installed them simply with commands from github

git clone https://github.com/glfw/glfw glfw
git clone https://github.com/nigels-com/glew.git glew

(since glew has no CMakeList inside the root directory, I had to write find_package instead of add_subdirectory)

My main Cmake looks like this:

cmake_minimum_required(VERSION 3.17)
project(test)

set(CMAKE_CXX_STANDARD 17)

# Source files
add_executable(test src/main.cpp)

# dependency
add_subdirectory(3rd-party)

find_package(OpenGL)

target_link_libraries(test PUBLIC opengl32 glfw glew)

And i got an error:

x86_64-w64-mingw32/bin/ld.exe: cannot find -lglew

The linker doesn't seem to find the glew library but I don't know why, (the IDE tells me that there are no errors in Cmake itself)

I would like to have 3rd part foolder in my project structure, it's more convenient for me.

How do I fix the error? I would like to say that I'm trying to make CMake as simple as possible.

CodePudding user response:

Quoting from the maintainer of the library who wrote in this GitHub issue:

For the purpose of a git submodule, I'd recommend using: Perlmint/glew-cmake rather than this upstream repository.

The long standing policy here is to maintain the history of the GLEW code generators, but not the corresponding history of the generated code. The general advice is to use the current GLEW release, rather than the in-progress master branch.

I do appreciate that this arrangement is old-fashioned, but indeed GLEW has been around for a good long while.

Note on your comment that

since glew has no CMakeList inside the root directory

It does have a CMakeLists.txt file here under :/build/cmake/. You can find discussion about its placement and the CMake situation of the library here.

As for your immediate problem, quoting from you in the comment section:

if I change GLEW::glew to libglew_static all works fine!

  • Related