SFML community forums

Help => General => Topic started by: texus on April 10, 2019, 07:03:20 pm

Title: undefined reference to GLX functions when using cmake with static libs on linux
Post by: texus on April 10, 2019, 07:03:20 pm
When linking static SFML libraries on linux, cmake doesn't automatically link to GLX (while it links fine to all other SFML dependencies).

The following simple cmake script is enough to reproduce it.
cmake_minimum_required(VERSION 3.5)
project(CMake-test)

set(SFML_STATIC_LIBRARIES TRUE)
find_package(SFML 2 COMPONENTS graphics window system REQUIRED)

add_executable(Test main.cpp)
target_link_libraries(Test PRIVATE sfml-graphics)

The main.cpp file is practically empty:
#include <SFML/Graphics.hpp>
int main()
{
    sf::RenderTexture target;
}

Running make will give the following output:
(click to show/hide)

Changing SFML_STATIC_LIBRARIES to FALSE fixes it, so it only occurs when linking statically.
Adding "GLX" to the target_link_libraries line also fixes the issue.

Edit: Linking to "GL" (instead of "GLX") also fixes it (but linking to "OpenGL" doesn't).
Title: Re: undefined reference to GLX functions when using cmake with static libs on linux
Post by: eXpl0it3r on April 12, 2019, 01:39:49 pm
Static linking on Linux is often not as recommended, but if a configuration issue in SFML is causing issues, then lets fix it!
Can you think of a fix?
Title: Re: undefined reference to GLX functions when using cmake with static libs on linux
Post by: texus on April 12, 2019, 07:20:35 pm
The fix seems to be to change
sfml_bind_dependency(TARGET OpenGL FRIENDLY_NAME "OpenGL" SEARCH_NAMES "OpenGL" "GL")
to
sfml_bind_dependency(TARGET OpenGL FRIENDLY_NAME "OpenGL" SEARCH_NAMES "GL" "OpenGL")
in SFMLConfigDependencies.cmake.in (just swapping the "GL" and "OpenGL" strings).

With the current code, the OpenGl_LIB variable contained "/usr/lib/libOpenGL.so" and I got linking errors to GLX.
After the change, OpenGl_LIB contained "/usr/lib/libGL.so" and there were no more linking errors.

The libGL.so file is also the one the SFML links to. The "find_package(OpenGL)" call provides both OPENGL_gl_LIBRARY and OPENGL_opengl_LIBRARY values, but OPENGL_gl_LIBRARY is the one that SFML uses for target_link_libraries.
Title: Re: undefined reference to GLX functions when using cmake with static libs on linux
Post by: binary1248 on April 12, 2019, 07:51:46 pm
One has to pay careful attention to the fact that the same script is used to link against EGL and all the other non-desktop-legacy-OpenGL stuff as well, this includes Android and friends. I am pretty sure that the CMake guys had good reasons to go their way with the GLVND change they implemented at some point, but this also means we need to take all the possible scenarios into account ourselves.

See: https://github.com/Kitware/CMake/blob/master/Modules/FindOpenGL.cmake