Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know library variable names for CMakeLists?

Tags:

c++

cmake

opengl

When using CMakeLists to compile an OpenGL project, I have the following line to link glut and gl:

target_link_libraries(my_exe ${OPENGL_gl_LIBRARY} ${GLUT_LIBRARIES})

I looked up how to link glut and gl with CMake so I saw that I could use ${OPENGL_gl_LIBRARY} and ${GLUT_LIBRARIES}. But how would I know the variables to use otherwise? I am used to just doing ${THELIBRARY_LIBRARES}, but in the case of gl, it changed to adding that "gl" into the variable name. How would I know that without googling it (for any library I want to use)?

like image 629
Sterling Avatar asked Jan 05 '23 08:01

Sterling


1 Answers

Besides consulting the find module's documentation, you could also use CMake's VARIABLES property to give you the variables that were defined by your find_package() call.

For an example the following code:

cmake_minimum_required(VERSION 3.2)

project(FindPackageVars)

get_directory_property(_vars_before VARIABLES)
find_package(OpenGL)
get_directory_property(_vars VARIABLES)

list(REMOVE_ITEM _vars _vars_before ${_vars_before})
foreach(_var IN LISTS _vars)
    message(STATUS "${_var} = ${${_var}}")
endforeach()

Outputs on my machine:

-- Found OpenGL: /usr/lib/x86_64-linux-gnu/libGL.so
-- FIND_PACKAGE_MESSAGE_DETAILS_OpenGL = [/usr/lib/x86_64-linux-gnu/libGL.so][/usr/include][v()]
-- OPENGL_FOUND = TRUE
-- OPENGL_GLU_FOUND = YES
-- OPENGL_INCLUDE_DIR = /usr/include
-- OPENGL_INCLUDE_PATH = /usr/include
-- OPENGL_LIBRARIES = /usr/lib/x86_64-linux-gnu/libGLU.so;/usr/lib/x86_64-linux-gnu/libGL.so
-- OPENGL_LIBRARY = /usr/lib/x86_64-linux-gnu/libGLU.so;/usr/lib/x86_64-linux-gnu/libGL.so
-- OPENGL_XMESA_FOUND = NO
-- OPENGL_gl_LIBRARY = /usr/lib/x86_64-linux-gnu/libGL.so
-- OPENGL_glu_LIBRARY = /usr/lib/x86_64-linux-gnu/libGLU.so
-- OPENGL_xmesa_INCLUDE_DIR = OPENGL_xmesa_INCLUDE_DIR-NOTFOUND
like image 93
Florian Avatar answered Jan 11 '23 20:01

Florian