CMake 声称找到 OpenGL 但找不到 EGL 和 OpenGL::GL

CMake claims finding OpenGL but can't find EGL and OpenGL::GL

在 Ubuntu.

上尝试查找 OpenGL 时,CMake 给我的结果令人困惑

上下文是我需要在服务器上进行无头渲染/docker 没有 X 显示。我已经通过 apt-get install libgl1-mesa-devapt-get install libegl1-mesa-dev.

安装了 OpenGL

这是我CMakeLists.txt中的相关部分:

cmake_minimum_required (VERSION 3.5.1)
project (sandbox LANGUAGES CXX)

# add OpenGL

find_package(OpenGL REQUIRED COMPONENTS OpenGL EGL GLX)
include_directories(${OPENGL_INCLUDE_DIRS})
if(OPENGL_FOUND)
    message("Found OpenGL in the current environment!")
else()
    message("Error: No OpenGL found.")
endif()

message("OpenGL include dirs" )
message("${OPENGL_INCLUDE_DIR}")
message("EGL include dirs" )
message("${OPENGL_EGL_INCLUDE_DIRS}")

if (OpenGL_EGL_FOUND)
    message("EGL Found!")
else()
    message("EGL Not Found!")
endif()

add_executable (sandbox "hello_egl.cpp" "shader.cpp")

target_link_libraries(sandbox PRIVATE OpenGL::OpenGL OpenGL::EGL OpenGL::GLX)

#target_include_directories(sandbox PRIVATE "/usr/include/EGL")
#target_link_libraries(sandbox "/usr/local/lib/x86_64-linux-gnu/libEGL.so")
#target_link_libraries(sandbox "/usr/local/lib/libEGL.so")

set_target_properties(sandbox PROPERTIES CXX_STANDARD 11)

这是我从 运行 cmake .. 得到的错误:

Found OpenGL in the current environment!
OpenGL include dirs
/usr/local/include
EGL include dirs

EGL Not Found!

-- Configuring done
CMake Error at CMakeLists.txt:44 (add_executable):
  Target "sandbox" links to target "OpenGL::OpenGL" but the target was not
  found.  Perhaps a find_package() call is missing for an IMPORTED target, or
  an ALIAS target is missing?


CMake Error at CMakeLists.txt:44 (add_executable):
  Target "sandbox" links to target "OpenGL::EGL" but the target was not
  found.  Perhaps a find_package() call is missing for an IMPORTED target, or
  an ALIAS target is missing?


CMake Error at CMakeLists.txt:44 (add_executable):
  Target "sandbox" links to target "OpenGL::GLX" but the target was not
  found.  Perhaps a find_package() call is missing for an IMPORTED target, or
  an ALIAS target is missing?

令人困惑的是CMakeOPENGL_FOUND设置为true,但不能link设置为目标OpenGL::OpenGL

此外,我在/usr/local/lib/x86_64-linux-gnu/中有EGL,但为什么CMake找不到它?

root@6442439c7090:/app/sandbox/build# ls /usr/local/lib/x86_64-linux-gnu/
libEGL.so        libGL.so.1         libGLESv1_CM.so.1.2.0  libGLX.so         libGLdispatch.so.0      libOpenGL.so.0.0.0
libEGL.so.1      libGL.so.1.7.0     libGLESv2.so           libGLX.so.0       libGLdispatch.so.0.0.0  pkgconfig
libEGL.so.1.1.0  libGLESv1_CM.so    libGLESv2.so.2         libGLX.so.0.0.0   libOpenGL.so
libGL.so         libGLESv1_CM.so.1  libGLESv2.so.2.1.0     libGLdispatch.so  libOpenGL.so.0

我坚持使用find_package的原因是之前我手动link到libEGL.so但是应用程序无法在运行时显示。所以我怀疑我link进错了库

terminate called after throwing an instance of 'std::runtime_error'
  what():  EGL error 0x300c at eglGetDisplay
Aborted (core dumped)

根据@Tsyvarev 的有用评论,此输出是由于 CMake 版本错误。

我忘了 link 它但是 FindOpenGL 我看到的 the documentation 是版本 3.18.1,而我要求最低 CMake 版本是 3.5.1。

在 3.5.1 中,没有 COMPONENTS 部分。 OPENGL_FOUND 也不会检查 EGL.