Building GLEW 1.7.0 on Windows using MinGW

Moritz Jasper picture Moritz Jasper · May 10, 2012 · Viewed 9.3k times · Source

It's been asked quite alot before: How do I compile the GLEW 1.7.0 source on Windows with MinGW? The goal being to dynamically link against the library from a c++ project.

More Info: I'm working with QtCreator, ergo use qmake for building. I'm on Windows 7. By now I have tried / had a look at the following links.

use posted batch file also tried to replace gcc with g++

static with vc++ libs, build dll.a reuse vc++ .dll

Get MSYS run shipped makefile

Info on initial issues

simple stuff using GLEW msvc++ binaries, works on my desktop

Unfortuantely all posted solutions end in the following error messages for me, when I use the compiled results in my project:

undefined reference to `glDrawElements@16'
debug/Ex04.o: In function `Z6initGLv':
undefined reference to `glClearColor@16'
undefined reference to `glEnable@4'
debug/Ex04.o: In function `Z8updateGLv':
undefined reference to `glClear@4'
undefined reference to `glViewport@16'
collect2: ld returned 1 exit status
mingw32-make.exe[1]: *** [debug/ecg4.exe] Error 1
mingw32-make.exe: *** [debug] Error 2

I'm at the end of my wits regarding this issue. I've double and triple checked the LIBS path in qmake and the windows path variable to include the directory where the glew dll's live. Also qmake's INCLUDEPATH should be fine. Here the paths in the .pro file anyways:

LIBS += -L$$quote(C:/mypath/freeglut/lib/) -lfreeglut
LIBS += -L$$quote(C:/mypath/glew-1.7.0/lib/) -lglew32 -lglew32mx
#LIBS+= C:/mypath/glew-1.7.0/lib/libglew32.dll.a
#LIBS+= C:/Programming/glew-1.7.0/lib/libglew32mx.dll.a

#includepath for project and the required libraries
INCLUDEPATH += ./include
INCLUDEPATH += "C:/mypath/glew-1.7.0/include"
INCLUDEPATH += "C:/mypath/freeglut/include"

So is there someone out there who could give a foolproof set of instructions about how to get the GLEW 1.7.0 source compiled with MinGW?

Answer

Moritz Jasper picture Moritz Jasper · May 11, 2012

Ok I solved it.

Basically I compiled GLEW correctly, as far as I can see. I forgot to add -lopengl32 -lglu32 to the LIBS path in my project. For some reason I need to do this on my Qt/MinGW/Windows system. On my Desktop: Qt/VC++/Windows I do not have to do this. Does anyone have an explanation for that?

For anyone stumbling across the same problem:

  1. Download source (.zip) from GLEW homepage
  2. Compile the source, there are different ways (cf. links in initial question)
    -> see bottom (old batch file isn't up on github any more)
  3. Put the .dll.a files in a directory which will be part of your LIBS path in qmake
  4. Put the .dll files into a directory which is part of the systemvariable PATH
  5. Don't forget to link with opengl, glu,glew in your qmake project file

Here's a snippet of my project file:

#Required Libraries, replace with your path
# $$quote(...) for quoting pathes with spaces

LIBS += -L$$quote(C:/Programming/freeglut/lib/) -lfreeglut
LIBS += -L$$quote(C:/Programming/glew-1.7.0/lib/) -lglew32 -lglew32mx
# the following line is not necessary when working with VS compiler, odd
LIBS += -lopengl32 -lglu32

#includepath for project and the required libraries
INCLUDEPATH += ./include
INCLUDEPATH += "C:/Programming/glew-1.7.0/include"
INCLUDEPATH += "C:/Programming/freeglut/include"

Anyways thanks alot, maybe this helps some people to not forget about dependencies :)

Cheers, Moritz


Edit: The batch file isn't up any more and I just had to compile glew 1.9.0. So here are some further notes:

  1. Make sure ar.exe and gcc.exe are on your Path. For the "QtSDK standard install" those programs can be found at C:\QtSDK\mingw\bin;
  2. Put the following lines into a batch file in the glew-1.x.y folder. This is a condensed version of LightningIsMyName's Answer

    gcc -DGLEW_NO_GLU -O2 -Wall -W -Iinclude  -DGLEW_BUILD -o src/glew.o -c src/glew.c
    gcc -shared -Wl,-soname,libglew32.dll -Wl,--out-implib,lib/libglew32.dll.a    -o lib/glew32.dll src/glew.o -L/mingw/lib -lglu32 -lopengl32 -lgdi32 -luser32 -lkernel32
    ar cr lib/libglew32.a src/glew.o
    
    gcc -DGLEW_NO_GLU -DGLEW_MX -O2 -Wall -W -Iinclude  -DGLEW_BUILD -o src/glew.mx.o -c src/glew.c
    gcc -shared -Wl,-soname,libglew32mx.dll -Wl,--out-implib,lib/libglew32mx.dll.a -o lib/glew32mx.dll src/glew.mx.o -L/mingw/lib -lglu32 -lopengl32 -lgdi32 -luser32 -lkernel32
    ar cr lib/libglew32mx.a src/glew.mx.o
    
  3. Run the batch file. The results will be in the lib/ folder. Resulting .dll and .dll.a files go together (.dll.a is what you want to tell your linker about, .dll should be available to the system at runtime). The .a files are for static linking.