简体   繁体   中英

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

CMake is giving me confusing results when trying to find OpenGL on Ubuntu.

The context is I need to do headless rendering on the server / docker without X display. I have installed OpenGL via apt-get install libgl1-mesa-dev and apt-get install libegl1-mesa-dev .

Here is the relevant part in my 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)

Here is the error I got from running 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?

What's confusing is that CMake sets OPENGL_FOUND to true , but can't link to the target OpenGL::OpenGL ?

Also, I have EGL in /usr/local/lib/x86_64-linux-gnu/ but why can't CMake find it?

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

The reason I insist to use find_package is that previously I manually link to libEGL.so but the application can't get display during runtime. So I suspect I link to the wrong library.

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

As per the helpful comment of @Tsyvarev, this output is due to the wrong version of CMake.

I forgot to link it but the documentation of FindOpenGL I look at is for version 3.18.1 while I required the min CMake version to be 3.5.1.

In 3.5.1, there's no COMPONENTS part. Also OPENGL_FOUND does not check for EGL .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM