6

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)
genpfault
  • 51,148
  • 11
  • 85
  • 139
yuqli
  • 4,461
  • 8
  • 26
  • 46
  • You could try giving a hint to tell CMake where to find the EGL library: `cmake -DOPENGL_egl_LIBRARY=/usr/local/lib/x86_64-linux-gnu/libEGL.so ..` Also, have you read the "Linux-specific" discussion in the CMake [docs](https://cmake.org/cmake/help/latest/module/FindOpenGL.html#linux-specific) about this package? – Kevin Aug 06 '20 at 00:21
  • 4
    Which **version** of CMake do you use? In 3.5 version (as requested by `cmake_minimum_required`) [FindOpenGL.cmake](https://cmake.org/cmake/help/v3.5/module/FindOpenGL.html) module doesn't locate EGL library and doesn't process `COMPONENTS` part. – Tsyvarev Aug 06 '20 at 07:39
  • @Tsyvarev Yes that's the problem. Thanks! I didn't realize there's a version differnce. – yuqli Aug 06 '20 at 16:49
  • @squareskittles Thanks for the comment! This is a helpful tip. I was just trying to let CMake find it because previously manual link resulted in unfavorable outcome... I did read the doc but read the wrong version... – yuqli Aug 06 '20 at 16:50

1 Answers1

3

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.

yuqli
  • 4,461
  • 8
  • 26
  • 46