Tuesday, May 25, 2010

Build FreeGlut with MinGW

Build FreeGlut with MinGW

(updated Jan 21, 2011: new FreeGlut 64-bit pre-compiled binary is now available. This solves a naming issue caused by an earlier version of MinGW. Thank you James for pointing out the issue.)

FreeGlut 2.6.0 comes with Visual Studio 2008 solution files. If you use Visual Studio, you can probably build the package easily. If you, however, want to use MinGW to build FreeGlut (either or both 32-bit and 64-bit versions), you may not know where to start. Thus, I wrote this document, so that you can build FreeGlut yourself. If you prefer, there are several binary packages ready to use in some web sites. Example of these web sites are http://www.transmissionzero.co.uk/software/freeglut-devel/ and this web site itself.

32-Bit FreeGlut

Use MSYS to build 32-bit freeglut shared library [ref: http://netsuperbrain.com/blog/posts/freeglut-windows-hopengl-hglut/]
  1. gcc -O2 -c -DFREEGLUT_EXPORTS *.c -I../include
  2. gcc -shared -o freeglut32.dll *.o -Wl,--enable-stdcall-fixup,--out-implib,libfreeglut32.a -lopengl32 -lglu32 -lgdi32 -lwinmm

Use MSYS to build 32-bit freeglut static library
  1. gcc -O2 -c -DFREEGLUT_STATIC *.c -I../include (same as above, except the flag is changed from FREEGLUT_EXPORTS to FREEGLUT_STATIC)
  2. ar rcs libfreeglut32_static.a *.o [ref: http://www.adp-gmbh.ch/cpp/gcc/create_lib.html]

If you want to use a pre-compiled binary package, instead of building it yourself, get a pre-compiled binary package here.

64-Bit FreeGlut

This tutorial show a way to build 64-bit FreeGlut by using the Windows command prompt (cmd.exe). Almost every thing is the same as using MSYS for the 32-bit version. In this case, I assume that the current directory is the freeglut source directory and MinGW x64 is at C:\MinGW_x64\bin\.

First of all, set PATH to MinGW in the command prompt:
set PATH=%PATH%;C:\mingw_x64\bin

For a shared library (note that actual prefix gcc-related file names may be different from distribution to distribution)
  1. x86_64-w64-mingw32-gcc -O2 -c -DFREEGLUT_EXPORTS *.c -I../include
  2. x86_64-w64-mingw32-gcc -shared -o freeglut64.dll *.o -Wl,--enable-stdcall-fixup,--out-implib,libfreeglut64.a -lopengl32 -lglu32 -lgdi32 -lwinmm

For a static library
  1. x86_64-w64-mingw32-gcc -O2 -c -DFREEGLUT_STATIC *.c -I../include (same as above, except the flag is changed from FREEGLUT_EXPORTS to FREEGLUT_STATIC)
  2. x86_64-w64-mingw32-ar rcs libfreeglut64_static.a *.o

If you want to use a pre-compiled binary package, instead of building it yourself, get a pre-compiled binary package here. This package was built with sezero's build MinGW GCC 4.4.5 20101001.

Pinyo Taeprasartsit
(May 2010, Jan 2011)

This document can be viewed at my blog and my Google docs. Please leave a message in my blog if you have any questions, comments, and suggestions. Thank you.

3 comments:

Anonymous said...

Thanks a lot this is very helpful.

PS said...

Build using cmd.exe? Gives me this:

C:\freeglut-2.8.0\src>x86_64-w64-mingw32-gcc -O2 -c -DFREEGLUT_EXPORTS *.c -I../include
x86_64-w64-mingw32-gcc: error: *.c: Invalid argument
x86_64-w64-mingw32-gcc: fatal error: no input files
compilation terminated.

Unknown said...

@Phil: Wildcards (*'s) don't seem to work with MinGW at a Windows command line. The workaround is to list all the files manually in the same invocation. I'll save you the time ;) (Note that this is for 64-bit! Substitute the right commands from Pinyo's examples for 32-bit)

For shared libs:

x86_64-w64-mingw32-gcc -O2 -c -DFREEGLUT_EXPORTS freeglut_callbacks.c freeglut_cursor.c freeglut_display.c freeglut_ext.c freeglut_font.c freeglut_font_data.c freeglut_gamemode.c freeglut_geometry.c freeglut_glutfont_definitions.c freeglut_init.c freeglut_input_devices.c freeglut_joystick.c freeglut_main.c freeglut_menu.c freeglut_misc.c freeglut_overlay.c freeglut_spaceball.c freeglut_state.c freeglut_stroke_mono_roman.c freeglut_stroke_roman.c freeglut_structure.c freeglut_teapot.c freeglut_videoresize.c freeglut_xinput.c freeglut_window.c -I../include

then:

x86_64-w64-mingw32-gcc -shared -o freeglut64.dll freeglut_callbacks.o freeglut_cursor.o freeglut_display.o freeglut_ext.o freeglut_font.o freeglut_font_data.o freeglut_gamemode.o freeglut_geometry.o freeglut_glutfont_definitions.o freeglut_init.o freeglut_input_devices.o freeglut_joystick.o freeglut_main.o freeglut_menu.o freeglut_misc.o freeglut_overlay.o freeglut_spaceball.o freeglut_state.o freeglut_stroke_mono_roman.o freeglut_stroke_roman.o freeglut_structure.o freeglut_teapot.o freeglut_videoresize.o freeglut_xinput.o freeglut_window.o -Wl,--enable-stdcall-fixup,--out-implib,libfreeglut64.a -lopengl32 -lglu32 -lgdi32 -lwinmm


For static libs:

x86_64-w64-mingw32-gcc -O2 -c -DFREEGLUT_STATIC freeglut_callbacks.c freeglut_cursor.c freeglut_display.c freeglut_ext.c freeglut_font.c freeglut_font_data.c freeglut_gamemode.c freeglut_geometry.c freeglut_glutfont_definitions.c freeglut_init.c freeglut_input_devices.c freeglut_joystick.c freeglut_main.c freeglut_menu.c freeglut_misc.c freeglut_overlay.c freeglut_spaceball.c freeglut_state.c freeglut_stroke_mono_roman.c freeglut_stroke_roman.c freeglut_structure.c freeglut_teapot.c freeglut_videoresize.c freeglut_xinput.c freeglut_window.c -I../include

then:

ar rcs libfreeglut64_static.a freeglut_callbacks.o freeglut_cursor.o freeglut_display.o freeglut_ext.o freeglut_font.o freeglut_font_data.o freeglut_gamemode.o freeglut_geometry.o freeglut_glutfont_definitions.o freeglut_init.o freeglut_input_devices.o freeglut_joystick.o freeglut_main.o freeglut_menu.o freeglut_misc.o freeglut_overlay.o freeglut_spaceball.o freeglut_state.o freeglut_stroke_mono_roman.o freeglut_stroke_roman.o freeglut_structure.o freeglut_teapot.o freeglut_videoresize.o freeglut_xinput.o freeglut_window.o


Should work for you as it did for me.