# How to direct FindX11 and FindOpenGL to look in specific directory

**URL:** <https://discourse.cmake.org/t/how-to-direct-findx11-and-findopengl-to-look-in-specific-directory/4790>\
**Category:** Code\
**Created:** [January 6, 2022, 11:59pm UTC](https://discourse.cmake.org/t/how-to-direct-findx11-and-findopengl-to-look-in-specific-directory/4790 "2022-01-06T23:59:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomoreilly](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/t/439d5e/32.png) [@tomoreilly](https://discourse.cmake.org/u/tomoreilly)\
**Post date:** [January 6, 2022, 11:59pm UTC](https://discourse.cmake.org/t/how-to-direct-findx11-and-findopengl-to-look-in-specific-directory/4790/1 "2022-01-06T23:59:41Z")

</div>

I am using cmake v 3.22.1.  
On our MacOS systems we want to use XQuartz for X11 and OpenGL includes and libs, which are installed in /opt/X11/include and /opt/X11/lib, respectively. My CMakeLists.txt calls FindOpenGL and FindX11, but the result is

```auto
X11_INCLUDE_DIR: /usr/local/include;/usr/X11R6/include
X11_LIBRARIES: /usr/local/lib/libSM.dylib;/usr/local/lib/libICE.dylib;/usr/local/lib/libX11.dylib;/usr/local/lib/libXext.dylib
OPENGL_INCLUDE_DIR: /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/System/Library/Frameworks/OpenGL.framework
OPENGL_LIBRARY: /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/System/Library/Frameworks/OpenGL.framework;/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/System/Library/Frameworks/OpenGL.framework

```

So those are the wrong places - they should be in /opt/X11.  
Based on FindX11 and FindOpenGL documentation I try this before calling the FindModules:

```auto
if (APPLE)
  set(X11_X11_INCLUDE_PATH /opt/X11/include/)
  set(X11_X11_LIB /opt/X11/lib/)
  set(OPENGL_INCLUDE_DIR /opt/X11/include/)
  set(OPENGL_gl_LIBRARY /opt/X11/lib/)
endif()

```

And the result is:

```auto
X11_INCLUDE_DIR: /opt/X11/include/;/usr/local/include;/usr/X11R6/include
X11_LIBRARIES: /opt/X11/lib/;/usr/local/lib/libXext.dylib
OPENGL_INCLUDE_DIR: /opt/X11/include/
OPENGL_LIBRARIES: /opt/X11/lib/;/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/System/Library/Frameworks/OpenGL.framework

```

I also get these warnings:

```auto
WARNING: Target "mbgsfShared" requests linking to directory "/opt/X11/lib/". Targets may link only to libraries. CMake is dropping the item.
WARNING: Target "mbgsfShared" requests linking to directory "/opt/X11/lib/". Targets may link only to libraries. CMake is dropping the item.
WARNING: Target "mbgrdviz" requests linking to directory "/opt/X11/lib/". Targets may link only to libraries. CMake is dropping the item.
WARNING: Target "mbgrdviz" requests linking to directory "/opt/X11/lib/". Targets may link only to libraries. CMake is dropping the item.
WARNING: Target "mbedit" requests linking to directory "/opt/X11/lib/". Targets may link only to libraries. CMake is dropping the item.
WARNING: Target "mbeditviz" requests linking to directory "/opt/X11/lib/". Targets may link only to libraries. CMake is dropping the item.
WARNING: Target "mbeditviz" requests linking to directory "/opt/X11/lib/". Targets may link only to libraries. CMake is dropping the item.
WARNING: Target "mbnavadjust" requests linking to directory "/opt/X11/lib/". Targets may link only to libraries. CMake is dropping the item.
WARNING: Target "mbnavadjust" requests linking to directory "/opt/X11/lib/". Targets may link only to libraries. CMake is dropping the item.
WARNING: Target "mbnavedit" requests linking to directory "/opt/X11/lib/". Targets may link only to libraries. CMake is dropping the item.
WARNING: Target "mbnavedit" requests linking to directory "/opt/X11/lib/". Targets may link only to libraries. CMake is dropping the item.
WARNING: Target "mbvelocitytool" requests linking to directory "/opt/X11/lib/". Targets may link only to libraries. CMake is dropping the item.
WARNING: Target "mbvelocitytool" requests linking to directory "/opt/X11/lib/". Targets may link only to libraries. CMake is dropping the item.

```

So how do I force FindX11 and FindOpenGL to look in /opt/X11/include and /opt/X11/lib, without generating these warnings?

Thanks  
Tom

---

<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:** [January 7, 2022, 1:26pm UTC](https://discourse.cmake.org/t/how-to-direct-findx11-and-findopengl-to-look-in-specific-directory/4790/2 "2022-01-07T13:26:03Z")

</div>

Well, you added the directory paths to the library lists, so those warnings are from your `_LIB` and `_LIBRARY` settings to containing paths directly. I think setting `X11_ROOT` and `OpenGL_ROOT` should help push `/opt/X11` to the front of the list. See the first bullet point of the search procedure in [the docs](https://cmake.org/cmake/help/latest/command/find_file.html).

---

<div class="post-metadata">

**Author:** ![tomoreilly](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/t/439d5e/32.png) [@tomoreilly](https://discourse.cmake.org/u/tomoreilly)\
**Post date:** [January 7, 2022, 4:22pm UTC](https://discourse.cmake.org/t/how-to-direct-findx11-and-findopengl-to-look-in-specific-directory/4790/3 "2022-01-07T16:22:37Z")

</div>

I see your point about the \_LIB/\_LIBRARY warnings.  
Prepending X11\_ROOT and OpenGL\_ROOT with /opt/X11 didn’t affect the FindX11 and FindOpenGL results (e.g. X11\_INCLUDE\_DIR: /usr/local/include;/usr/X11R6/include):

```auto
if (APPLE)
  message("MACOS: assume X11 and OpenGL are in /opt/X11")
  set(CMAKE_FIND_USE_PACKAGE_ROOT_PATH TRUE)
  set(X11_ROOT /opt/X11/ ${X11_ROOT})
  set(OpenGL_ROOT /opt/X11/ ${OpenGL_ROOT})
endif()

```

Here’s the actual content of /opt/X11, which contains the X11/OpenGL includes and libraries:

```auto
% ls /opt/X11
bin/ include/	libexec/	var/
etc/ lib/ share/

```

Moreover I see the [FindOpenGL documentaton](https://cmake.org/cmake/help/latest/module/FindOpenGL.html) says

> 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

Does this mean that FindOpenGL will never find OpenGL stuff in /opt/X11 on OSX?

---

<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:** [January 8, 2022, 11:55am UTC](https://discourse.cmake.org/t/how-to-direct-findx11-and-findopengl-to-look-in-specific-directory/4790/4 "2022-01-08T11:55:51Z")

</div>

Hmm, it would seem so. There might be a (silent) policy associated with `_ROOT` variables, but it isn’t mentioned.

---

<div class="post-metadata">

**Author:** ![neundorf](https://discourse.cmake.org/user_avatar/discourse.cmake.org/neundorf/32/2075_2.png) [@neundorf](https://discourse.cmake.org/u/neundorf)\
**Post date:** [January 9, 2022, 9:54pm UTC](https://discourse.cmake.org/t/how-to-direct-findx11-and-findopengl-to-look-in-specific-directory/4790/5 "2022-01-09T21:54:47Z")

</div>

Setting CMAKE\_PREFIX\_PATH to /opt/X11 should work I think.

Alex
