Take the 2-minute tour ×

I am developing static library that takes screenshots, and taking them from OpenGL applications require special handling.

When client application links to my static library it have to add frameworks used by my library, for example to take OpenGL screenshots, even if client app is not using OpenGL it have to link with OpenGLES.framework which is bad. I am trying to check in the library if client have linked with OpenGLES.framework and dynamically enable taking screenshots from OpenGL.

The problem is I get compilation error when I try to use C functions like:

if(&glReadPixels != NULL) {

As you can see, I can check for method existence, but how do I invoke it to not cause linker error? When I compile client with my library I get this:

Undefined symbols for architecture i386:
"_glReadPixels", referenced from:
  +[TakeScreenshotUtil takeOpenGLScreenshotWithContext:layerSize:] in libScr-iOS.a(TakeScreenshotUtil.o)

I am trying to use

__attribute__ ((weak)) 

but it doesn't work (doesn't change anything).

share|improve this question
Why is linking against OpenGLES.framework bad? –  Richard J. Ross III May 21 '12 at 12:01
I don't want to force client apps to link with it if they do not use it. –  Piotr Wach May 21 '12 at 12:44

2 Answers 2

up vote 1 down vote accepted

You can open app being executed and check if it links to OpenGL. First, recompile your app with -rdynamic (or whatever equivalent Apple's GCC understands). Then use the following code to find a function:

#import <dlfcn.h>

void (*_glReadPixels)(int, int, float, float, int, int, void *);
_glReadPixels = dlsym(NULL, "glReadPixels");
if (_glReadPixels != NULL) {
    /* take screenshot */
share|improve this answer
Thanks! Works great :) –  Piotr Wach May 23 '12 at 9:38

Here is a link to a great set of code for screenshotting without using the OpenGL libraries. Even OpenGL views begin with a UIView, thus if you are doing Full Screen screenshots this should still work. http://www.icodeblog.com/2009/07/27/1188/

Also, If it is screenshots of portions of an OpenGL view that you are after, then I would suggest that you still use the code referenced above, and simply crop it down to what you need / want, else you could go down the path of getting a partial capture of the pixels within the OpenGL View. If you are doing this though, then the user of your API is already using OpenGL and thus most likely linking to the framework anyway.

share|improve this answer
We use this method already, but it isn't that simple. Views that have embedded OpenGL content do not render correctly using it (I mean they do not render at all). Documentation says: "[...]Important The Mac OS X v10.5 implementation of this method does not support the entire Core Animation composition model. QCCompositionLayer, CAOpenGLLayer, and QTMovieLayer layers are not rendered.[...]" –  Piotr Wach May 23 '12 at 8:37

Your Answer


