Building glew for Mac OSX

42,382

Solution 1

The engine uses modern opengl (shader based) and so requires GLEW

GLEW is not a prerequisite for using modern OpenGL features. It is a method to load extended functionality, not the only one. You're on MacOS X so, the extension system is of little use for you anyway, because the OpenGL version supported is entirely determined by the OS and the available framework. Apple develops the OpenGL drivers themself and the extensions they provide are only those, that are for features not found in the OpenGL specification (i.e. vendor specific and EXT). All you need is a version of the OpenGL Framework new enough. Any MacOS X released after 2006 can do it.

Solution 2

You can use Homebrew to do the work for you:

brew install glew

Solution 3

Update 20180912 - don't use GLEW. It's just not worth the trouble...


I've installed GLEW using from source using:

make GLEW_DEST="$HOME/local" OPT="-march=core2 -O2" install.all

Your optimizations and destinations may vary. This yields a libGLEW.a, libGLEWmx.a, and their dylib counterparts in $HOME/local/lib. The pkgconfig sub-directory will also include glew.pc. GLEW doesn't always play well with others either. Having to set globals like glewExperimental, as well as needing to be careful with the order of inclusion of other headers: <OpenGL/gl3.h> or <glfw.h>, etc.

If I can offer some advice - forget SDL for modern GL programming. If you want to get into pure, modern GL on OS X . - consider something like:

#if defined (__APPLE_CC__)
#include <OpenGL/gl3.h>
#else
#include <GL/gl3.h>       /* assert OpenGL 3.2 core profile available. */
#endif

#define GLFW_INCLUDE_GL3  /* don't drag in legacy GL headers. */
#define GLFW_NO_GLU       /* don't drag in the old GLU lib - unless you must. */

#include <GL/glfw.h>      /* simple and easy to use. omit legacy GL. */

Installing GLFW is pretty easy too:

> env PREFIX="$HOME/local" CFLAGS="-march=core2 -O2" make cocoa cocoa-dist-install

Again, your settings may differ.


20160412: My comments about SDL are out of date, as others have pointed out. Particularly if you're working on yet-another-engine. I maintain that glfw3 is an easier way to make modern OpenGL apps on OSX. It's very well written and maintained code, with a (correctly!) narrow focus on GL and event handling. Obviously, from an 'engine' perspective, SDL2 is providing portable extended video APIs, audio APIs, thread APIs, etc. Though with C11 and C++11, even a thread API abstraction layer is perhaps redundant.

GLEW is problematic in that in can't handle multiple contexts without extensive user intervention. This isn't an issue for OSX, which provides a core profile API and doesn't need to load 'function points'. IIRC, GL on X (DRI?) has this too - but I might be wrong about that. There are other loaders out there. I'm still waiting for one to rule them all, which ensures different functions pointers are loaded for multiple contexts if necessary, and uses a thread-local (or even a pthread key) for correct dispatch, that can be generated from the latest gl.xml from Khronos, or whoever...


20180912

I think the easiest approach right now is using GLFW to provide a context, and use GLAD to resolve GL function pointers. The only downside is that there's no effective management of multiple GL contexts. This is what GLFW is currently using in its demos... I would forget about GLEW completely at this point.

Solution 4

MacPorts should get those libraries and their dependencies installed for you:

sudo port install glew +universal
sudo port install libsdl +universal

When building your game, it's best to call g++ for both the building and linking steps; you just need to refer to /opt/local for it to find the headers and libraries that have been installed by MacPorts. You can also statically-link the .a files if you'd like:

g++ -c -O3 -I/opt/local/include main.cpp -o main.o
g++ main.o /opt/local/lib/libSDL.a /opt/local/lib/libSDLmain.a /opt/local/lib/libGLEW.a -o mygame

If you're building from the command-line, I recommend using a Makefile and building with make.

Solution 5

I would recommend going to http://glew.sourceforge.net and downloading the ZIP file right onto your Mac. Then go into the Finder and type in glew.h to see the file, control click it, get info, and copy the path to the file. Then go into your respective program file (e.g., commons.h) that contains the #include <GL/glew.h> dependency, and change it to:

#include <path/to/file/that/you/just/copied/glew.h>  

Then, try to compile your program again, and this time you should not get any GLEW errors.

Share:
42,382
DavidColson
Author by

DavidColson

A programmer, game developer and physicist with an obsession with space.

Updated on July 05, 2022

Comments

  • DavidColson
    DavidColson almost 2 years

    I have spent the day struggling to get my simple engine to work on Mac. I have SDL working and now the only thing giving trouble is opengl. The engine uses modern opengl (shader based) and so requires GLEW. I have tried everything from fink to MacPorts to install it and nothing works.

    The most success I have had has been building it from source. First I got an error saying 'GL/glu.h' no such file or directory found. So I renamed the includes to OpenGL/glu.h and that fixed that issue. But now I get this error ld: unknown option: -shared I am completely stuck at this point.

    Also Id rather a static build if anyone knows how to do that.

  • noway
    noway over 10 years
    "Forget SDL for modern OpenGL programming". This comment is now out of date. SDL2 lets you specify the version of the OpenGL context.
  • michaelgulak
    michaelgulak almost 9 years
    As someone working on a hobby project that uses OpenGL and SDL, it is difficult to develop a good understanding of the OpenGL landscape. I am using SDL2 with OpenGL, and am trying to load shaders for the first time. However, it's difficult to tell which version of OpenGL I'm even using. Can you (@mlabbe or otherwise) suggest any resources that would help with this? Mostly I find narrow tutorials.
  • noway
    noway almost 9 years
    I recommend the revision of Open GL superbible that corresponds to the version of OpenGL you are targeting. It focuses on the core profile, which eliminates legacy calls. This is much more effective than randomly Googling. docs.gl is also useful for matching calls to versions.
  • Efren
    Efren over 8 years
    How would a program including GLEW be ported to compile on OS X?
  • datenwolf
    datenwolf over 8 years
    @Efren: Check for the __APPLE__ preprocessor macro and only if that one's not been defined include the GLEW header. And for the case __APPLE__ is defined, define (empty) macros that turn glew… function calls into no-ops.
  • Efren
    Efren over 8 years
    So when not including the GLEW header, what apple header is available? I mean, the old program is actually calling those functions.
  • datenwolf
    datenwolf over 8 years
    @Efren: Just OpenGL/GL.h, it's all there without extra work required. The upside of this is, that one does not have to deal with function pointer loading. The downside is, that the OpenGL version being used immediately sets a hard minimum requirement on the MacOS X version your program requires.
  • Efren
    Efren over 8 years
    Thanks, the problem was that the old program is also requiring GLUT
  • miker
    miker about 8 years
    The sample header here was helpful... but for Xcode on mac osX 10.11 (in 2016), I had to use "#include <GLFW/glfw3.h>" for the final line instead of the "#include <GL/glfw.h>" that is listed here.
  • Rafael Eyng
    Rafael Eyng about 8 years
    Then what? I'm totally new to this and I'm trying to make this work. The installation did complete, but I'm still clueless about what to do next.
  • Ankit Panda
    Ankit Panda almost 7 years
    @RafaelEyng after this write your code in say main.cpp then compile using clang++ main.cpp -lGLEW -framework OpenGL -o <EXECUTABLE_FILE> ( and if you use other libraries you could add -lglfw3 -framework Cocoa -framework OpenGL -framework IOKit -framework CoreVideo ).
  • Samie Bencherif
    Samie Bencherif about 5 years
    You really should not do it this way
  • Mahesh Jamdade
    Mahesh Jamdade almost 2 years
    This should be the right answer. Works fine on mac m1