Alex Beutel's Blog

CMake, OpenGL, and GLX on OS X

July 28th, 2010 · 4 Comments

As you may know, I am staying at Duke this summer for research.  My research, which I started last semester and am going to be doing for a while, focuses on scalable algorithms.  While that is pretty broad, I am currently working on using the graphics card to speed up processing. Again, here are some technical notes on code.

In doing my work I have come to have to write and run C++ OpenGL code on both OS X and Ubuntu.  To make this easier, CMake has been great for both  having readable make files and for being able to keep the files consistent across both platforms.  Additionally, while GLUT is great for cross-platform OpenGL coding, I needed more control over the environment. Being able to run X11 on OS X has allowed me to test GLX natively without always testing on Linux.  However, to get this set up on OS X without XCode can be a little tricky.  So, two snippets of CMake files for using OpenGL on OS X and Linux.

If you want to run OpenGL with the standard OS X libraries, CMake generally finds the correct frameworks, but it can’t hurt to specify them more precisely.  A basic example of the CMakeLists is shown below:

   INCLUDE_DIRECTORIES ( /System/Library/Frameworks )
target_link_libraries(main ${EXTRA_LIBS})

With this, your C++ should have the following includes:

#include <GLUT/glut.h>
#include <OpenGL/glext.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>

If, however, you are looking to run your code also on a Linux machine or simply want to use GLX rather than CGL, you will need to have X11 installed (which your OS X installation disk contains) and specifically point CMake at the X11 libraries.  The CMake code for this is shown below:

target_link_libraries(main ${EXTRA_LIBS})

When using the X11 libraries, you must also change the includes as the folder structure for X11 is different than that natively for OS X:

#include <GL/glew.h>
#include <GL/glut.h>
#include <GL/glext.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glx.h>

That is all.  Not terribly complicated, but hopefully this saves you some time if you are doing development with CMake, OpenGL, or X11.  Look for some new slightly more exciting posts soon.

Tags: C++ · OpenGL · Programming

4 responses so far ↓

  • 1 Per // Oct 5, 2011 at 12:59 am

    Thanks man! You saved my day 🙂

  • 2 Calum // Jun 26, 2012 at 2:21 pm

    Ah that saved some time, thanks!

  • 3 Nils // Aug 10, 2015 at 8:33 pm

    Could you believe, this blog post is helping me out, three years later…Question though, for that second CMake code block, should it be inside of an IF…ENDIF just like the first one?


  • 4 从零开始手敲次世代游戏引擎(macOS特别篇) – 文礼简阔 // Dec 2, 2017 at 8:11 am

    […] CMake, OpenGL, and GLX on OS X […]

Leave a Comment