Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: undefined reference to GLX functions when using cmake with static libs on linux  (Read 1497 times)

0 Members and 1 Guest are viewing this topic.

texus

  • Sr. Member
  • ****
  • Posts: 484
    • View Profile
    • TGUI
    • Email
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).
« Last Edit: April 10, 2019, 07:19:00 pm by texus »
TGUI: C++ SFML GUI
TGUI.Net: C# binding for TGUI

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 9841
    • View Profile
    • development blog
    • Email
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?
Official FAQ: https://www.sfml-dev.org/faq.php
Nightly Builds: https://www.nightlybuilds.ch/
——————————————————————
Dev Blog: https://dev.my-gate.net/
Thor: http://www.bromeon.ch/libraries/thor/

texus

  • Sr. Member
  • ****
  • Posts: 484
    • View Profile
    • TGUI
    • Email
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.
TGUI: C++ SFML GUI
TGUI.Net: C# binding for TGUI

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1398
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
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
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).