【发布时间】:2021-08-13 13:55:31
【问题描述】:
我有一个非常简单的 CMakeLists.txt,我在其中使用 OpenGL,并且需要与旧版 OpenGL 链接。
策略CMP0072 说我需要将OpenGL_GL_PREFERENCE 设置为LEGACY。
cmake_minimum_required(VERSION 3.10)
project(Foo)
set(OpenGL_GL_PREFERENCE LEGACY)
find_package(OpenGL REQUIRED)
set(SOURCES
main.cpp)
add_executable(foo
${SOURCES})
target_link_libraries(
foo
PUBLIC OpenGL::GLU
)
但是,cmake 的输出是:
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenGL: /usr/lib/x86_64-linux-gnu/libOpenGL.so
-- Configuring done
-- Generating done
-- Build files have been written to: /home/alessandro/tmp/foo/build
它正在链接libOpenGL.so 而不是libGL.so。
此外,如果我评论 set(OpenGL_GL_PREFERENCE LEGACY) 行,我会得到以下输出:
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Warning (dev) at /usr/share/cmake-3.16/Modules/FindOpenGL.cmake:275 (message):
Policy CMP0072 is not set: FindOpenGL prefers GLVND by default when
available. Run "cmake --help-policy CMP0072" for policy details. Use the
cmake_policy command to set the policy and suppress this warning.
FindOpenGL found both a legacy GL library:
OPENGL_gl_LIBRARY: /usr/lib/x86_64-linux-gnu/libGL.so
and GLVND libraries for OpenGL and GLX:
OPENGL_opengl_LIBRARY: /usr/lib/x86_64-linux-gnu/libOpenGL.so
OPENGL_glx_LIBRARY: /usr/lib/x86_64-linux-gnu/libGLX.so
OpenGL_GL_PREFERENCE has not been set to "GLVND" or "LEGACY", so for
compatibility with CMake 3.10 and below the legacy GL library will be used.
Call Stack (most recent call first):
CMakeLists.txt:6 (find_package)
This warning is for project developers. Use -Wno-dev to suppress it.
-- Found OpenGL: /usr/lib/x86_64-linux-gnu/libOpenGL.so
-- Configuring done
-- Generating done
-- Build files have been written to: /home/alessandro/tmp/foo/build
这很令人困惑:它首先说默认情况下更喜欢使用GLVND,然后
为了与 CMake 3.10 及以下版本兼容,将使用旧版 GL 库。
无论如何,libGL.so 和 libOpenGL.so 都已安装,libOpenGL.so 已设置。
我不知道问题出在哪里,以及如何强制 cmake 使用旧版 GL 而不是 GLVND。
运行 ubuntu 20.04,但在 ubuntu 18.04 中的行为也是相同的。
【问题讨论】:
-
似乎第一个变体应该可以工作,但在这种情况下 CMake 输出令人困惑:CMake 找到了 OpenGL 的 both 流,但第一个实例是
OPENGL_opengl_LIBRARY,所以它打印在-- Found OpenGL: /usr/lib/x86_64-linux-gnu/libOpenGL.so。您应该能够使用变量OPENGL_gl_LIBRARY或目标OpenGL::GL来访问旧版 OpenGL。 -
可能,在FindOpenGL.cmake 中用
list(PREPEND _OpenGL_REQUIRED_VARS OPENGL_gl_LIBRARY)替换list(APPEND _OpenGL_REQUIRED_VARS OPENGL_gl_LIBRARY)应该可以解决这个令人困惑的输出。 -
谢谢@Tsyvarev。所以,如果我理解正确的话,有两个目标:
OpenGL::GL和OpenGL::OpenGL。因此,OpenGL::OpenGL无论如何都是 GLVND,OpenGL::GL可能是旧版或 GLVND,具体取决于政策?而Found OpenGL:print 与这两个目标无关?这很令人困惑,也没有很好的记录.. -
目标
OpenGL::OpenGL和OpenGL::GL在documentation 中描述。至于Found XXX: ...输出的内容,我没有看到任何模块(CMake 附带的模块和外部模块都没有)记录该输出。