Cannot use legacy GL library with cmake (ubuntu 18.04 and 20.04)

Viewed 2493

I have a very simple CMakeLists.txt, where I use OpenGL and I need to link with legacy OpenGL. Policy CMP0072 says that I need to set OpenGL_GL_PREFERENCE to 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
    )

However, cmake output is:

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

It is linking libOpenGL.so instead of libGL.so.

Moreover, if I comment the line set(OpenGL_GL_PREFERENCE LEGACY), I get the following output:

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

which is quite confusing: it first says that prefers to use GLVND by default, but then

for compatibility with CMake 3.10 and below the legacy GL library will be used.

In any case, both libGL.so and libOpenGL.so are installed, and libOpenGL.so is set.

I cannot figure out where could be the issue, and how can I force cmake to use legacy GL instead of GLVND.

Running ubuntu 20.04, but the behaviour is the same also in ubuntu 18.04.

1 Answers

Add this to your CMakeLists.txt file.

set(OpenGL_GL_PREFERENCE LEGACY)
Related