When compiling GLEW or freeglut as a static library…

When compiling GLEW or freeglut as a static library…

Surprised nobody ran across this yet, but then again, makes sense.

When compiling either the freeglut and GLEW (packages nearly essential of OpenGL graphics development), both have the option to be built as libraries statically or dynamically.  When the libraries are built dynamically, you just include the lib in the link line then copy the dll’s over to your binary folder.  But when compiled statically, you need to specify GLEW_STATIC and FREEGLUT_STATIC symbols as compile-time defines when compiling the libraries (and be free of dll hell).

The tricky part is when you include those two libraries in another library and then try to compile that combination into a binary.  In the case of freeglut, the linker will complain that it can’t find “freeglut.lib”.  Which is strange because freeglut static compiles into a library named “freeglut_static.lib” and only the dll version compiles into “freeglut.lib”.   Hmmm, why is freeglut looking for the wrong lib?  Well, freeglut_std.h line 53 holds the key.   If FREEGLUT_STATIC is defined, then there is a pragma to include “freeglut_static.lib” otherwise the pragma points to “freeglut.lib”.  So, you need to include the -D FREEGLUT_STATIC in the link line of the final executable’s link line as well.

GLEW also has this problem, but if you forget the -D GLEW_STATIC in the link line of the executable, you’ll get errors about unresolved externals.

And there you have it.

(update: you must also put  GLEW_STATIC and FREEGLUT_STATIC in the link lines of the intermediate libraries as well.  The chain of libraries from freeglut and glew ALL need to have that compile parameter specified or you’ll get the same errors).

3 thoughts on “When compiling GLEW or freeglut as a static library…

  1. Hey, where exactly do you put the FREEGLUT_STATIC symbol and how do you write it? I can compile a dll but not a .lib, it would be greatly appreciated if you could help me out

    1. Depends on the build tool you’re using. In a linux-style makefile, you add it as a -D parameter at the command line. For Visual Studio, you have to do it in the project settings, C/C++, Preprocessor – and then add the FREEGLUT_STATIC line to the Preprocessor Definitons. Then do a clean on the project and rebuild all.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.