I am trying to compile a relatively simple OpenGL program using MinGW on a Win 7 x64 system, and I keep getting undefined references to several of the GLEW functions. I hav
You need to #define GLEW_STATIC
before #include "glew.h"
if you are using the static linking library. I would go ahead and add a rule to your Makefile to define this pre-processor token, rather than actually putting the #define ...
in your source code.
This is mentioned in the installation documentation for GLEW, by the way. But judging by the number of times this question is asked, it may not be stated clearly enough.
The reason for defining this token is that Microsoft Windows uses a special __declspec (...)
for DLL imports and exports. By defining GLEW_STATIC
, you are telling the linker to use standard behavior to locate the symbols in your .lib
.
When GLEW_STATIC
is undefined, it informs the linker that the library's symbols are resolved at runtime. But MSVC needs to know whether it is creating exports or looking for imports and so there is another token GLEW_BUILD
that defines this behavior. Since you want to link to (import), and not build (export) GLEW, make sure you do not define GLEW_BUILD
.
/*
* GLEW_STATIC is defined for static library.
* GLEW_BUILD is defined for building the DLL library.
*/
#ifdef GLEW_STATIC
# define GLEWAPI extern
#else
# ifdef GLEW_BUILD
# define GLEWAPI extern __declspec(dllexport)
# else
# define GLEWAPI extern __declspec(dllimport)
# endif
#endif
It is also worth mentioning that you cannot use the pre-built dynamic linking .lib
and DLL files that are supplied on the official GLEW website. They are compiled using MSVC; to use a DLL compiled with MSVC in MinGW see this link. The better solution is just to avoid using the dynamic link library and use the static library.