How to know library variable names for CMakeLists?

2019-01-12 10:56发布

问题:

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)?

回答1:

Those variables are obtained via find_package(XXX) calls.

Such calls are redirected, depended from the library, either to FindXXX.cmake script (shipped with CMake or contained in the project which uses it) or to XXXConfig.cmake script (shipped with the library itself).

So, for determine meaningful variable's names you need to consult appropriate script. Usually, interface of the script (input-output variables) is described in comments at the beginning of the script.

Documentation for FindXXX.cmake scripts shipped with CMake may be read in CMake documentation pages about modules.



回答2:

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


回答3:

You don't. It is dependent on the find-module for the library.

See here.

Under Writing find modules you see that variables are set in the module. When checking the FindOpenGL.cmake module in your CMake-Modules directory you will see the name of the variable.



标签: c++ opengl cmake