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.
The main.cpp file is practically empty:
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderTexture target;
}
Running make will give the following output:
Linking CXX executable Test
/usr/bin/ld: /usr/local/lib/libsfml-window-s.a(GlxContext.cpp.o): in function `sf::priv::GlxContext::~GlxContext()':
GlxContext.cpp:(.text+0x28): undefined reference to `glXGetCurrentContext'
/usr/bin/ld: GlxContext.cpp:(.text+0x3a): undefined reference to `glXDestroyContext'
/usr/bin/ld: GlxContext.cpp:(.text+0x71): undefined reference to `glXDestroyPbuffer'
/usr/bin/ld: GlxContext.cpp:(.text+0xa9): undefined reference to `glXMakeCurrent'
/usr/bin/ld: /usr/local/lib/libsfml-window-s.a(GlxContext.cpp.o): in function `sf::priv::GlxContext::makeCurrent(bool)':
GlxContext.cpp:(.text+0xf9): undefined reference to `glXMakeCurrent'
/usr/bin/ld: GlxContext.cpp:(.text+0x118): undefined reference to `glXMakeContextCurrent'
/usr/bin/ld: GlxContext.cpp:(.text+0x140): undefined reference to `glXMakeCurrent'
/usr/bin/ld: /usr/local/lib/libsfml-window-s.a(GlxContext.cpp.o): in function `sf::priv::GlxContext::~GlxContext()':
GlxContext.cpp:(.text+0x198): undefined reference to `glXGetCurrentContext'
/usr/bin/ld: GlxContext.cpp:(.text+0x1aa): undefined reference to `glXDestroyContext'
/usr/bin/ld: GlxContext.cpp:(.text+0x1e9): undefined reference to `glXDestroyPbuffer'
/usr/bin/ld: GlxContext.cpp:(.text+0x219): undefined reference to `glXMakeCurrent'
/usr/bin/ld: /usr/local/lib/libsfml-window-s.a(GlxContext.cpp.o): in function `sf::priv::GlxContext::selectBestVisual(_XDisplay*, unsigned int, sf::ContextSettings const&)':
GlxContext.cpp:(.text+0x510): undefined reference to `glXGetConfig'
/usr/bin/ld: GlxContext.cpp:(.text+0x531): undefined reference to `glXGetConfig'
/usr/bin/ld: GlxContext.cpp:(.text+0x546): undefined reference to `glXGetConfig'
/usr/bin/ld: GlxContext.cpp:(.text+0x55b): undefined reference to `glXGetConfig'
/usr/bin/ld: GlxContext.cpp:(.text+0x570): undefined reference to `glXGetConfig'
/usr/bin/ld: /usr/local/lib/libsfml-window-s.a(GlxContext.cpp.o):GlxContext.cpp:(.text+0x585): more undefined references to `glXGetConfig' follow
/usr/bin/ld: /usr/local/lib/libsfml-window-s.a(GlxContext.cpp.o): in function `sf::priv::GlxContext::createSurface(sf::priv::GlxContext*, unsigned int, unsigned int, unsigned int)':
GlxContext.cpp:(.text+0xb9b): undefined reference to `glXQueryVersion'
/usr/bin/ld: GlxContext.cpp:(.text+0xbc7): undefined reference to `glXChooseFBConfig'
/usr/bin/ld: GlxContext.cpp:(.text+0xbe8): undefined reference to `glXGetVisualFromFBConfig'
/usr/bin/ld: GlxContext.cpp:(.text+0xc5d): undefined reference to `glXCreatePbuffer'
/usr/bin/ld: /usr/local/lib/libsfml-window-s.a(GlxContext.cpp.o): in function `sf::priv::GlxContext::createContext(sf::priv::GlxContext*)':
GlxContext.cpp:(.text+0xdf7): undefined reference to `glXQueryDrawable'
/usr/bin/ld: GlxContext.cpp:(.text+0xe36): undefined reference to `glXChooseFBConfig'
/usr/bin/ld: GlxContext.cpp:(.text+0xf4c): undefined reference to `glXQueryVersion'
/usr/bin/ld: GlxContext.cpp:(.text+0xfb8): undefined reference to `glXChooseFBConfig'
/usr/bin/ld: GlxContext.cpp:(.text+0xfe2): undefined reference to `glXGetVisualFromFBConfig'
/usr/bin/ld: GlxContext.cpp:(.text+0x10ad): undefined reference to `glXMakeCurrent'
/usr/bin/ld: GlxContext.cpp:(.text+0x10c9): undefined reference to `glXCreateContext'
/usr/bin/ld: GlxContext.cpp:(.text+0x1220): undefined reference to `glXGetVisualFromFBConfig'
/usr/bin/ld: GlxContext.cpp:(.text+0x13a0): undefined reference to `glXMakeCurrent'
/usr/bin/ld: /usr/local/lib/libsfml-window-s.a(GlxContext.cpp.o): in function `sf::priv::GlxContext::display()':
GlxContext.cpp:(.text+0x16d): undefined reference to `glXSwapBuffers'
/usr/bin/ld: /usr/local/lib/libsfml-window-s.a(GlxContext.cpp.o): in function `sf::priv::GlxContext::getFunction(char const*)':
GlxContext.cpp:(.text+0x411): undefined reference to `glXGetProcAddressARB'
/usr/bin/ld: /usr/local/lib/libsfml-window-s.a(GlxExtensions.cpp.o): in function `sfglx_LoadFunctions':
GlxExtensions.cpp:(.text+0x1bf): undefined reference to `glXQueryExtensionsString'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/Test.dir/build.make:92: Test] Error 1
make[1]: *** [CMakeFiles/Makefile2:73: CMakeFiles/Test.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
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).
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.