c++cmakeglm-math

GLM linking in CMakeLists.txt


I cannot link glm library with my executable. I tried link via ${GLM_INCLUDE_DIRS}, ${GLM_LIBRARIES} and ${GLM_LIBRARY_DIRS} cmake variables but it does not work.

How can I link libraries and inludes of glm with my executable?

I am using find_package() method :

find_package(glm REQUIRED PATHS "${GLM_BINARY_DIR}" NO_DEFAULT_PATH)

And does not have any problem with find_package() but these status messages below displays nothing :

message(STATUS "GLM includes ${GLM_INCLUDE_DIRS}")
message(STATUS "GLM libraries ${GLM_LIBRARY_DIRS}")

Solution

  • Config script for GLM defines IMPORTED target glm::glm. So the correct way for use GLM in CMake code is to link with that target. This is explicitly written in the documentation:

    set(glm_DIR <installation prefix>/lib/cmake/glm) # if necessary
    find_package(glm REQUIRED)
    target_link_libraries(<your executable> glm::glm)
    

    Variables like GLM_LIBRARIES or GLM_INCLUDE_DIRS are usually defined by Find scripts (shipped with CMake or with the consumer project).

    Config scripts, shipped with the installed package, normally defines IMPORTED targets.

    GLM doesn't have a Find script (see FindGLM.cmake not in glm 0.9.7, is it a deprecated way to find libraries in CMAKE?), and PATHS option in find_package invocation explicitly requests Config script.