Building glew for Mac OSX
Asked Answered
P

7

21

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.

Paronychia answered 1/9, 2012 at 17:5 Comment(6)
I would suggest using a ports system like macports, fink, homebrew as the authors will have done the work of porting the build process to OSX. In this case OSX linker does not understand -shared so you need to use its equivalent and there will be several other problemsMinor
as I stated I used Macports and it sayed it had succeeded but theres nothing installedParonychia
What did sudo port install glew give as an error?Minor
It didnt, I just found out how it installs things but I am still unable to link with glewParonychia
should just need -L/opt/local/lib and -lglew (assuming library is libglew.dylib) so just a different directory from what you do if you built it yourselfMinor
All I had to do was specify opt/local/include as a search directory in Codeblocks and it worked!Paronychia
G
16

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.

Gelhar answered 1/9, 2012 at 18:19 Comment(5)
How would a program including GLEW be ported to compile on OS X?Infinitive
@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.Gelhar
So when not including the GLEW header, what apple header is available? I mean, the old program is actually calling those functions.Infinitive
@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.Gelhar
Thanks, the problem was that the old program is also requiring GLUTInfinitive
B
32

You can use Homebrew to do the work for you:

brew install glew
Bettyannbettye answered 13/8, 2013 at 23:21 Comment(2)
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.Plate
@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 ).Aglet
G
16

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.

Gelhar answered 1/9, 2012 at 18:19 Comment(5)
How would a program including GLEW be ported to compile on OS X?Infinitive
@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.Gelhar
So when not including the GLEW header, what apple header is available? I mean, the old program is actually calling those functions.Infinitive
@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.Gelhar
Thanks, the problem was that the old program is also requiring GLUTInfinitive
G
7

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.

Grin answered 3/9, 2012 at 17:22 Comment(4)
"Forget SDL for modern OpenGL programming". This comment is now out of date. SDL2 lets you specify the version of the OpenGL context.Postliminy
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.Stopoff
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.Postliminy
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.Compton
P
3

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.

Pointtopoint answered 13/8, 2013 at 22:49 Comment(0)
G
3

None of the answers above gave me what I was looking for: a quick way to setup and Xcode project with a static Glew dependency.

  1. Download GLEW source from here http://glew.sourceforge.net/
  2. Unzip the directory and from the root execute
$sudo make install 

(this installs the .dylib if you want to link dynamically located in lib/). You need to have CMake installed for the above command to run. if not already installed, then you can install using

brew install cmake
  1. Copy the .a static library from /usr/local/lib/ to your XCode project
  2. Update the "Header Search Paths" in your Xcode build settings for your target to include the headers (usually placed in /usr/local/include). Or just copy the headers over to your own project.
Goggles answered 15/8, 2017 at 20:23 Comment(1)
This should be the right answer. Works fine on mac m1Specification
W
1

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.

Wandering answered 29/10, 2014 at 4:27 Comment(1)
You really should not do it this wayGoebel
S
0

Maybe you should try this command brew install --build-from-source glew.

Sanatorium answered 16/6, 2022 at 14:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.