3

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 have set the libraries to link to the programs, and have been looking around for any library I may be missing from my list, yet the output from the linker still looks like this:

16:35:50 **** Incremental Build of configuration Debug for project test ****
Info: Internal Builder is used for build
gcc -LD:/DEV/openGL/lib/x86 -LD:/DEV/x86/lib -o test.exe test.o -lfreeglut -lglaux -lglew32s -lglu32 -lglfw3 -lopengl32 -lgdi32 
test.o: In function `init':
E:\Development\C\test\Debug/../test.c:32: undefined reference to `_imp____glewGenVertexArrays'
E:\Development\C\test\Debug/../test.c:33: undefined reference to `_imp____glewBindVertexArray'
E:\Development\C\test\Debug/../test.c:35: undefined reference to `_imp____glewGenBuffers'
E:\Development\C\test\Debug/../test.c:36: undefined reference to `_imp____glewBindBuffer'
E:\Development\C\test\Debug/../test.c:37: undefined reference to `_imp____glewBufferData'
test.o: In function `display':
E:\Development\C\test\Debug/../test.c:45: undefined reference to  `_imp____glewBindVertexArray'
test.o: In function `main':
E:\Development\C\test\Debug/../test.c:61: undefined reference to `_imp__glewInit@0'
collect2: ld returned 1 exit status

16:35:50 Build Finished (took 675ms)

I have tried with both -lglew32 and -lglew32s in several different configurations, thinking that perhaps there were definitions in glew32s that were not in glew32, and this did not help. Any guidance as to what I may be missing, or something I have overlooked?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Caleb Waggoner
  • 147
  • 1
  • 2
  • 13

1 Answers1

8

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.


UPDATE:

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.

Andon M. Coleman
  • 42,359
  • 2
  • 81
  • 106
  • I have trouble making your response the answer to this question. First, it only covers linkage with glew32s and not glew32, therefore not completely answering the question. Second, you have down voted the question because of your annoyance, disregarding any effort that I put into looking for a solution before asking. Thirdly, you disregard the lack of understanding that comes from simple unfamiliarity with the module being used. Not everyone likes OpenGL, because "DirectX is easier." I am not that person, but I hope for understanding, not scorn, for making mistakes while trying to learn. – Caleb Waggoner Aug 28 '13 at 05:16
  • @CalebWaggoner If it makes you feel any better, I was not the person who down-voted your question. You clearly demonstrated effort in solving this problem and explained steps you took, as far as I am concerned there is nothing wrong with your question. I would not have written an answer otherwise. As for building a dynamic library, please read the installation document -- I had hoped that explaining the `GLEW_STATIC` token and pointing you to the installation instructions would be enough for you to spot the `GLEW_BUILD` token in the documentation. – Andon M. Coleman Aug 28 '13 at 05:46
  • 1
    Thank you for filling in more detail regarding this issue, as this will help the next person who comes and asks this same question. Having come from using the MSDN a lot I've become a little leery about API documentation, but it seems that I will have to work on that as well. Also, sorry for accusing you of the down vote. Again, thank you. – Caleb Waggoner Aug 28 '13 at 13:17
  • 1
    What tricked me for a while is that I had to define GLEW_STATIC both when compiling the static glew library _and_ in the project that uses the static library. – Aberrant Aug 19 '14 at 12:45