Home > Blockchain >  GLM linking in CMakeLists.txt
GLM linking in CMakeLists.txt

Time:05-21

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}")

CodePudding user response:

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.

CodePudding user response:

My approach was wrong.

From OpenGL GLM offical page:

OpenGL Mathematics (GLM) is a header only C mathematics library for graphics software based on the OpenGL Shading Language (GLSL) specifications.

So, I just need to specify the /include path of builded GLM library in which the linker should search for libraries when linking a given target.

That can be handled by cmake target_include_directories() function. This fixed my problem :

target_include_directories(myExecutable "${GLM_BINARY_DIR}/include")

${GLM_BINARY_DIR} is defined by me and points to the where GLM binaries are stored.

  • Related