Building Glew on Windows with Mingw

Building glew on windows with mingw

To build it with MinGW, you should do (copied from the make log, with slight modifications and additional explanations):

mkdir lib/
mkdir bin/
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

# Create library file: lib/libglew32.dll.a
ar cr lib/libglew32.a src/glew.o

# Create pkg-config file (optional if you just want a lib)
sed \
-e "s|@prefix@|/usr|g" \
-e "s|@libdir@|/usr/lib|g" \
-e "s|@exec_prefix@|/usr/bin|g" \
-e "s|@includedir@|/usr/include/GL|g" \
-e "s|@version@|1.6.0|g" \
-e "s|@cflags@||g" \
-e "s|@libname@|GLEW|g" \
< glew.pc.in > glew.pc

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

# Create library file: lib/libglew32mx.dll.a
ar cr lib/libglew32mx.a src/glew.mx.o

# Create pkg-config file (optional if you just want a lib)
sed \
-e "s|@prefix@|/usr|g" \
-e "s|@libdir@|/usr/lib|g" \
-e "s|@exec_prefix@|/usr/bin|g" \
-e "s|@includedir@|/usr/include/GL|g" \
-e "s|@version@|1.6.0|g" \
-e "s|@cflags@|-DGLEW_MX|g" \
-e "s|@libname@|GLEWmx|g" \
< glew.pc.in > glewmx.pc

# Make the glew visualinfo program. Skip this if you want just the lib
gcc -c -O2 -Wall -W -Iinclude -o src/glewinfo.o src/glewinfo.c
gcc -O2 -Wall -W -Iinclude -o bin/glewinfo.exe src/glewinfo.o -Llib -lglew32 -L/mingw/lib -lglu32 -lopengl32 -lgdi32 -luser32 -lkernel32
gcc -c -O2 -Wall -W -Iinclude -o src/visualinfo.o src/visualinfo.c
gcc -O2 -Wall -W -Iinclude -o bin/visualinfo.exe src/visualinfo.o -Llib -lglew32 -L/mingw/lib -lglu32 -lopengl32 -lgdi32 -luser32 -lkernel32

You should then have a lib folder and a bin folder with the desired executables and libraries

Building glew on windows with mingw32

I have performed many searches in order to find an answer to my problems. It took a lot of time so I am posting it here to help others.

To make GLEW work with MinGW you should download the source from GLEW website and put

gcc.exe from MinGW\bin

ar.exe from MinGW32\mingw32\bin

to GLEW's source folder and create and run a .bat in that folder like that:

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

you will get your .dll and .a files in lib folder. Put .dll files to system32 folder and put .a files to MinGW lib folder.

Finally, if you are using SFML, link SFML libraries before GLEW and at last link OpenGL. If you change the linking order you will get a linker error.

Don't forget to call glewInit() after creating your window.

Trouble building GLEW on Windows using MinGW

I could never build it with the instructions I found around the internet, so here's how I build glew (using https://downloads.sourceforge.net/project/glew/glew/2.2.0/glew-2.2.0.tgz) under MSYS2 with MinGW-w64:

# change the line below to your desired install path
INSTALLPREFIX=/usr/local
VERSION=2.2.0
gcc -O2 -Wall -W -Iinclude -DGLEW_BUILD -o src/glew.o -c src/glew.c &&
gcc -fno-builtin -fno-stack-protector -shared -s -Wl,-soname,libglew32.dll -Wl,--out-implib,lib/libglew32.dll.a -o lib/glew32.dll src/glew.o -Wl,--as-needed -lglu32 -lopengl32 -lgdi32 -luser32 -lkernel32 -nostdlib &&
ar cr lib/libglew32.a src/glew.o &&
gcc -DGLEW_MX -O2 -Wall -W -Iinclude -DGLEW_BUILD -o src/glew.mx.o -c src/glew.c &&
gcc -fno-builtin -fno-stack-protector -shared -s -Wl,-soname,libglew32mx.dll -Wl,--out-implib,lib/libglew32mx.dll.a -o lib/glew32mx.dll src/glew.mx.o -Wl,--as-needed -lglu32 -lopengl32 -lgdi32 -luser32 -lkernel32 -nostdlib &&
ar cr lib/libglew32mx.a src/glew.mx.o &&
gcc -O2 -c -Iinclude -o src/glewinfo.o src/glewinfo.c &&
gcc -s -o src/glewinfo.exe src/glewinfo.o lib/libglew32.dll.a -Wl,--as-needed -lgdi32 -lopengl32 &&
gcc -O2 -c -Iinclude -o src/visualinfo.o src/visualinfo.c &&
gcc -s -o src/visualinfo.exe src/visualinfo.o lib/libglew32.dll.a -Wl,--as-needed -lgdi32 -lopengl32 -lglu32 &&
sed -e "s?@prefix@?$INSTALLPREFIX?; s?@libdir@?\$\{prefix\}/lib?; s?@version@?$VERSION?; s?@requireslib@?glu?; s?@cflags@??; s?@libname@?glew32?" glew.pc.in > glew.pc &&
sed -e "s?@prefix@?$INSTALLPREFIX?; s?@libdir@?\$\{prefix\}/lib?; s?@version@?$VERSION?; s?@requireslib@?glu?; s?@cflags@?-DGLEW_MX?; s?@libname@?glew32mx?" glew.pc.in > glewmx.pc &&
echo Installing... &&
mkdir -p $INSTALLPREFIX/include $INSTALLPREFIX/lib/pkgconfig $INSTALLPREFIX/bin $INSTALLPREFIX/cmake &&
cp -rf include/GL $INSTALLPREFIX/include/ &&
cp -f lib/*.a $INSTALLPREFIX/lib/ &&
cp -f *.pc $INSTALLPREFIX/lib/pkgconfig/ &&
cp -f lib/*.dll src/*.exe $INSTALLPREFIX/bin/ &&
cp -f build/cmake/*.cmake $INSTALLPREFIX/cmake/ &&
echo Success

How to use GLEW with MinGW

You could try GLee which essentially does the same thing as GLEW.

Building GLEW 1.7.0 on Windows using MinGW

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.

How to add a custom library (e.g. glew) to mingw-w64?

When using a library use the -I compiler flag to tell the compiler where to find the include files (in your case the path containing the GL folder) and the -L linker flag to tell the linker where to find the libraries.

To link with the library use the -l flag. The library itself is a a lib*.a file (or lib*.dll.a for shared libraries). For the -l flag the library is specified without prefix and suffix, so if your library is called libglew.a the flag will be -lglew.

It is also possible to specified the full path to the lib*.a file instead of -L and -l flags, and with MinGW, if you have the .dll file you can even try to specify the path of the .dll file and the linker will know what to do.

Compile Glew git code with MinGW on Windows

The problem probably comes from the fact that you use make from MinGW (your mingw32-make) but perl from MSYS (which ships with Git for Windows). So what you'd need is an MSYS make.

Let me point out a common misconception before I continue: msysgit is the name of development environment to create the Git for Windows installer (also see this answer). So usually people who just want to use Git on Windows should download Git for Windows, not msysgit. msysgit ships with all sorts of development stuff that is usually not needed, like gcc and (tadaa) make. So in you case, you'd really need msysgit, and I'd recommend the net installer from here. After the installation you should be able to launch C:\msysgit\msys.bat, change to your glew directory, and run make extensions.

How to link GLEW and GLFW and OpenGL to MingW's g++

The MingGW and mingw-w64
compilers are Windows commandline tools. They are Windows ports of (some of)
the GCC compilers. tdm-gcc is yet another choice.

Commandine tools for any operating
system don't need their own shell. You can run them in any shell you've got
on that operating system. On Windows today
you have a choice of at least PowerShell and the old CMD shell.

The MinGW project provides, as well as its GCC toolchain, a minimal unix-like
environment for Windows called MSYS, which includes a shell. You don't need
MSYS to run the compiler.

As long as the GCC tools can be located in
the value of the PATH environment variable that is operative in
the shell at compiletime, then you run the compiler at the shell prompt:

>gcc [options...]
>g++ [options...]

the same way it is run on any operating system. If you want to work with a GCC toolchain then the question:

How can I do all of the things I did in visual studio 2019 from Power Shell?

is simply the question, How do you run GCC? That's a question of
sweeping generality. You need to study relevant books
and documentation

Very sketchily, if you want to compile and link a C++ program that has source files main.cpp and other.cpp and depends on libraries
foo and bar that have their C++ APIs defined in header files and are implemented in DLLs,
you will do it with commands of the following form:

To compile the source files to object files:

>g++ -c -o main.obj main.cpp -I/path/to/foo/header/files -I/path/to/bar/header/files [any other compilation options....]
>g++ -c -o other.obj other.cpp -I/path/to/foo/header/files -I/path/to/bar/header/files [any other compilation options....]

To link the object files and libraries to make an excutable program:

>g++ -o prog main.obj other.obj -L/path/to/foo_dll -L/maybe/a/different/path/to/bar_dll -lfoo -lbar [any other linkage options...]

And if all that is successful then the program will be prog.exe and you can run it:

>prog

just like you ran g++, provided that foo.dll and bar.dll can be found at runtime by the OS loader's DLL search protocol

As I think you appreciate, in real life nobody builds programs by typing the all the
commands in a shell except for instructional purposes. They use a build system or an IDE to automate it. But it is true that building programs though the medium of a build system or IDE presents fewer difficulties if you do it with a basic grasp of how the tools behind it work.



Related Topics



Leave a reply



Submit