# X11 and OpenGL builds on MacOS

**URL:** https://discourse.cmake.org/t/x11-and-opengl-builds-on-macos/9466
**Category:** Code
**Created:** [November 20, 2023, 2:17am UTC](https://discourse.cmake.org/t/x11-and-opengl-builds-on-macos/9466 "2023-11-20T02:17:19Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![rmfritz](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/r/b487fb/32.png) [@rmfritz](https://discourse.cmake.org/u/rmfritz)
#### Post date: [November 20, 2023, 2:17am UTC](https://discourse.cmake.org/t/x11-and-opengl-builds-on-macos/9466/1 "2023-11-20T02:17:19Z")

</div>

Ok…I’m working with an old software package that still uses X11, and OpenGL via X11 on MacOS. X11 and openGL are deprecated anyway, but meantime we’d like to keep the software going.

First problem: FindX11 consistently finds the wrong directories. In a MacOS environment using [XQuartz](https://www.xquartz.org/), the X11 system is always in /opt/X11, and is symlinked at /usr/X11 and /usr/X11R6. In addition, I have Homebrew on the system, and it has some of the X11 libraries, but I don’t want to use them at all. find\_package(X11) gives a jumble of references to /usr/X11R6, /usr/local/lib/X11, and /usr/local/include/X11. Short of explicitly calling out the appropriate directories, is there a way to direct find\_package(X11) to use /opt/X11 only?

Second problem: according to the docs, “On OSX FindOpenGL defaults to using the framework version of OpenGL. People will have to change the cache values of OPENGL\_glu\_LIBRARY and OPENGL\_gl\_LIBRARY to use OpenGL with X11 on OSX.” Is there any way I can include this in the CMake scripts? Should I just explicitly call out libraries I want on MacOS?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [November 20, 2023, 11:59am UTC](https://discourse.cmake.org/t/x11-and-opengl-builds-on-macos/9466/2 "2023-11-20T11:59:26Z")

</div>

Does setting the `X11_ROOT` and `OpenGL_ROOT` variables to `/opt/X11` and whatever prefix OpenGL lives under work? You can use `cmake --debug-find` to get better information about search details.

---

<div class="post-metadata">

### Author: ![rmfritz](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/r/b487fb/32.png) [@rmfritz](https://discourse.cmake.org/u/rmfritz)
#### Post date: [November 20, 2023, 8:05pm UTC](https://discourse.cmake.org/t/x11-and-opengl-builds-on-macos/9466/3 "2023-11-20T20:05:57Z")

</div>

Wow, thanks for the quick reply!

Yes, setting X11\_ROOT to /opt/X11 works (yay!), but setting OpenGL\_ROOT does not (sigh.) For onlookers, the code for X11\_ROOT is:

```auto
cmake_policy(SET CMP0074 NEW) # cmake 3.12 or later
[…]
  if(APPLE)
    set(X11_ROOT "/opt/X11")
  endif()
  find_package(X11)

```

Unfortunately,

```auto
   if(APPLE AND X11_FOUND)
     set(OpenGL_ROOT "/opt/X11")
   endif()
   find_package(OpenGL)

```

Still finds the framework versions of the OpenGL libraries, rather than the ones under /opt/X11.

I’ve just noticed some CMake files in the XQuartz distribution; maybe they’re what I need. I’ll experiment further.

---

<div class="post-metadata">

### Author: ![rmfritz](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/r/b487fb/32.png) [@rmfritz](https://discourse.cmake.org/u/rmfritz)
#### Post date: [November 20, 2023, 8:17pm UTC](https://discourse.cmake.org/t/x11-and-opengl-builds-on-macos/9466/4 "2023-11-20T20:17:41Z")

</div>

No luck; those files are apparently for build scripts. Any further thoughts?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [November 20, 2023, 8:28pm UTC](https://discourse.cmake.org/t/x11-and-opengl-builds-on-macos/9466/5 "2023-11-20T20:28:05Z")

</div>

Did `--debug-find` help at all?

---

<div class="post-metadata">

### Author: ![rmfritz](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/r/b487fb/32.png) [@rmfritz](https://discourse.cmake.org/u/rmfritz)
#### Post date: [November 20, 2023, 11:22pm UTC](https://discourse.cmake.org/t/x11-and-opengl-builds-on-macos/9466/6 "2023-11-20T23:22:44Z")

</div>

It did. And the answer is the XQuartz include directory and library file names are not what FindOpenGL.cmake expects. In addition, cmake prioritizes frameworks over plain libraries. So the fix is:

1. Tell cmake not to look for frameworks
2. Fix FindOpenGL.cmake to look for the proper include file and library names.

Now, when I copy my modified FindOpenGL to a local directory, it cannot find FindPackageHandleStandardArgs.cmake. Do I need to copy that also, and whatever it includes? Is there perhaps a variable that gives the default module directory? Or…?

My code in CMakeLists.txt now reads:

```auto
   if(APPLE AND X11_FOUND)
     set(CMAKE_FIND_FRAMEWORK NEVER)
     set(OpenGL_ROOT "/opt/X11")
     find_package(OpenGL)
     set(CMAKE_FIND_FRAMEWORK "")
   else()
     find_package(OpenGL)
   endif()

```

Here’s a diff of a version of FindOpenGL.cmake which works when in the Modules directory of the app:

```auto
210,214c210,216
< # The OpenGL.framework provides both gl and glu
< find_library(OPENGL_gl_LIBRARY OpenGL DOC "OpenGL library for OS X")
< find_library(OPENGL_glu_LIBRARY OpenGL DOC
< "GLU library for OS X (usually same as OpenGL library)")
< find_path(OPENGL_INCLUDE_DIR OpenGL/gl.h DOC "Include for OpenGL on OS X")
---
> # The OpenGL.framework provides both gl and glu in OpenGL
> # XQuartz provides libgl and libglu
> find_library(OPENGL_gl_LIBRARY NAMES OpenGL GL DOC
> "OpenGL library for OS X")
> find_library(OPENGL_glu_LIBRARY NAMES OpenGL GLU DOC
> "GLU library for OS X")
> find_path(OPENGL_INCLUDE_DIR NAMES OpenGL/gl.h GL/gl.h DOC "Include for OpenGL on OS X")

```

---

<div class="post-metadata">

### Author: ![rmfritz](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/r/b487fb/32.png) [@rmfritz](https://discourse.cmake.org/u/rmfritz)
#### Post date: [November 21, 2023, 12:22am UTC](https://discourse.cmake.org/t/x11-and-opengl-builds-on-macos/9466/7 "2023-11-21T00:22:27Z")

</div>

And, yes, if I copy FindPackageMessage.cmake and FindPackageHandleStandardArgs.cmake to my local module directory, it all works, but what an awful hack, and one which may fail with future versions of CMake. Any suggestions?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [November 21, 2023, 3:03am UTC](https://discourse.cmake.org/t/x11-and-opengl-builds-on-macos/9466/8 "2023-11-21T03:03:30Z")

</div>

Edit those files to just do `include(ModName)`; they probably have `include(${CMAKE_ROOT}/ModName.cmake)` instead.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [November 21, 2023, 3:04am UTC](https://discourse.cmake.org/t/x11-and-opengl-builds-on-macos/9466/9 "2023-11-21T03:04:42Z")

</div>

> [@rmfritz](#):
>
> My code in CMakeLists.txt now reads:

The diffs to the Find module seem reasonable to submit as [an MR](https://gitlab.kitware.com/cmake/cmake/-/merge_requests). See [the contributing docs](https://gitlab.kitware.com/cmake/cmake/-/blob/master/CONTRIBUTING.rst).

---

<div class="post-metadata">

### Author: ![rmfritz](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/r/b487fb/32.png) [@rmfritz](https://discourse.cmake.org/u/rmfritz)
#### Post date: [November 21, 2023, 4:05am UTC](https://discourse.cmake.org/t/x11-and-opengl-builds-on-macos/9466/10 "2023-11-21T04:05:09Z")

</div>

And, indeed, removing the directory name and “.cmake” makes CMake consider it a module and find it in the Modules directory. Thank you.

---

<div class="post-metadata">

### Author: ![rmfritz](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/r/b487fb/32.png) [@rmfritz](https://discourse.cmake.org/u/rmfritz)
#### Post date: [November 21, 2023, 4:07am UTC](https://discourse.cmake.org/t/x11-and-opengl-builds-on-macos/9466/11 "2023-11-21T04:07:41Z")

</div>

Fine point: what’s common practice for naming a local modules directory? I used CMakeModules at the top level of the project, but I’m not sure that’s a good idea; it looks like it might be part of CMake instead of a local directory. Is there a common practice or style recommendation?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [November 21, 2023, 4:27am UTC](https://discourse.cmake.org/t/x11-and-opengl-builds-on-macos/9466/12 "2023-11-21T04:27:51Z")

</div>

I use `cmake` if I have a choice. Maybe `cmake/modules` if there’s some difference between `include`-able or not.

---

<div class="post-metadata">

### Author: ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)
#### Post date: [November 21, 2023, 8:00am UTC](https://discourse.cmake.org/t/x11-and-opengl-builds-on-macos/9466/13 "2023-11-21T08:00:39Z")

</div>

Yeah, `cmake` or `CMake` is pretty common.  
I typically have some sort of hierarchy underneath.

---

<div class="post-metadata">

### Author: ![rmfritz](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/r/b487fb/32.png) [@rmfritz](https://discourse.cmake.org/u/rmfritz)
#### Post date: [November 21, 2023, 10:08pm UTC](https://discourse.cmake.org/t/x11-and-opengl-builds-on-macos/9466/14 "2023-11-21T22:08:19Z")

</div>

Thanks - I’ll try that.

---

<div class="post-metadata">

### Author: ![rmfritz](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/r/b487fb/32.png) [@rmfritz](https://discourse.cmake.org/u/rmfritz)
#### Post date: [November 24, 2023, 11:17pm UTC](https://discourse.cmake.org/t/x11-and-opengl-builds-on-macos/9466/15 "2023-11-24T23:17:52Z")

</div>

And…it all works. Thanks for all the help.

Maybe next week I’ll get a Round Tuit and submit an MR. Meantime, here’s my FindOpenGL.cmake which supports XQuartz for anyone who needs it.

[FindOpenGL.cmake](https://discourse.cmake.org/uploads/short-url/bKbXze2r6Nf0Gz8JEsq42vPeMES.cmake) (25.2 KB)

---

<div class="post-metadata">

### Author: ![rmfritz](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/r/b487fb/32.png) [@rmfritz](https://discourse.cmake.org/u/rmfritz)
#### Post date: [December 10, 2023, 1:59am UTC](https://discourse.cmake.org/t/x11-and-opengl-builds-on-macos/9466/16 "2023-12-10T01:59:04Z")

</div>

MR submitted.
