openglcmakelinker-errorsx11glx

How to link against libGL on OSX using cmake?


I'm trying to compile camera_calibration on OSX 10.11 and after a few hurdles with a few X11 related dependencies I find myself still stuck with a few linking errors:

Undefined symbols for architecture x86_64:
  "_glXChooseVisual", referenced from:
      vis::OpenGLContextGLX::InitializeWindowless(vis::OpenGLContextImpl*) in opengl_context_glx.cc.o
  "_glXCreateContext", referenced from:
      vis::OpenGLContextGLX::InitializeWindowless(vis::OpenGLContextImpl*) in opengl_context_glx.cc.o
  "_glXDestroyContext", referenced from:
      vis::OpenGLContextGLX::Deinitialize() in opengl_context_glx.cc.o
  "_glXGetCurrentContext", referenced from:
      vis::IsOpenGLContextAvailable() in opengl.cc.o
      vis::OpenGLContextGLX::AttachToCurrent() in opengl_context_glx.cc.o
  "_glXGetCurrentDisplay", referenced from:
      vis::OpenGLContextGLX::AttachToCurrent() in opengl_context_glx.cc.o
  "_glXGetCurrentDrawable", referenced from:
      vis::OpenGLContextGLX::AttachToCurrent() in opengl_context_glx.cc.o
  "_glXMakeCurrent", referenced from:
      vis::OpenGLContextGLX::MakeCurrent() in opengl_context_glx.cc.o
ld: symbol(s) not found for architecture x86_64

I found libGL.dylib has these symbols:

for lib in /usr/X11R6/lib/*.dylib;do nm -gU $lib | grep _glXChooseVisual;done
000000000000307f T _glXChooseVisual
000000000000307f T _glXChooseVisual
gp:src George$ for lib in /usr/X11R6/lib/*.dylib;do echo $lib && nm -gU $lib | grep _glXChooseVisual;done
/usr/X11R6/lib/libAppleWM.7.dylib
/usr/X11R6/lib/libAppleWM.dylib
/usr/X11R6/lib/libFS.6.dylib
/usr/X11R6/lib/libFS.dylib
/usr/X11R6/lib/libGL.1.dylib
000000000000307f T _glXChooseVisual
/usr/X11R6/lib/libGL.dylib
000000000000307f T _glXChooseVisual

However I can't seem to tweak CMakeLists.txt to take this into account.

I've tried adding:

set(X11R6_INCLUDE_DIRS "/usr/X11R6/include")
set(X11R6_LIBRARIES "/usr/X11R6/lib")

which I later use when calling target_include_directories and target_link_libraries for libvis, but doesn't seem to do the trick.

Hackily adding -lGL in target_link_libraries results in ld: library not found for -lGL

I've also looked at CMake's FindOpenGL reference and tried using find_package(OpenGL REQUIRED COMPONENTS OpenGL GLX) but didn't get very far:

CMake Error at /usr/local/Cellar/cmake/3.18.2/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:165 (message):
  Could NOT find OpenGL (missing: GLX)

Any tips on correctly linking against GLX via CMake on OSX ?


Solution

  • On macOS do not(!) develop against X11 / GLX. They are not natively supported! The X11 server for macOS supports only indirect GLX with limited functionality. You will get only sub par performance. And CUDA (used by libvis) is not supported.

    On macOS all OpenGL development should use the native OpenGL framework: https://developer.apple.com/library/archive/documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/opengl_contexts/opengl_contexts.html

    However take note that OpenGL has been declared deprecated by Apple.

    In short: You'll at least have to modify your program to use the macOS OpenGL framework, or even bite the bullet and accept that macOS is not a well supported platform for your application.