Go And OpenGL With Windows and MinGW
Dec 20, 2014
3 minute read

As a follow-up to my previous article about using OpenGL in Go, this article describes how to get the sample running in Windows.

Requirements

  • Go 1.4 / 386
  • CMake

64-bit gcc isn’t implemented in MinGW, so the 32-bit version of Go must be used.

MinGW, GLEW and GLFW will be installed in the next section.

Compiling Dependencies

The first thing is to make MinGW compatible versions of GLEW and GLFW.

Installing MinGW

  1. download the mingw-get-setup.exe installer from the MinGW website (tested with the 2013-10-04 version).
  2. run mingw-get-setup.exe … when selecting package, make sure to flag the bases, g++ and developer tools packages and then Apply Changes from the Installation menu.
  3. run the shell by executing C:\mingw\msys\1.0\msys.bat
  4. run in the shell: export PATH=/c/MinGW/bin:$PATH

This gives you the MinGW shell with the PATH environment variable updated to include the bin directory for it so that it can locate the compilers.

Also note that this article assumes you’ve installed it to C:\MinGW which is the recommended default.

Installing GLEW

Installing GLEW with MinGW is straight forward and doesn’t require much effort.

  1. download glew-1.11.0 from the GLEW website.
  2. unzip the source archive and cd to that directory in the same MinGW shell
  3. run: make all
  4. run: make install

This will put the libraries into /usr/lib/ which means basically it goes to C:\MinGW\msys\1.0\lib.

Installing GLFW3

The process to install GLFW with MinGW is a little more complicated involving some extra settings to the CMake file.

  1. download glfw 3.0.4 from the GLFW website
  2. unzip the source archive and cd to that directory in the same MinGW shell
  3. run: mkdir build; cd build to create directory to hold the build output
  4. run: cmake -G "MSYS Makefiles" -DCMAKE_INSTALL_PREFIX:PATH=/c/MinGW/msys/1.0 -DBUILD_SHARED_LIBS=TRUE ..
  5. run: make
  6. run: make install

At this point everything should be compiled and installed, with GLFW built as a DLL. Unfortunately we need these libraries to have the lib prefix in their name. I couldn’t find an option in CMake for this, so my solution was to just create file links using the MinGW shell:

cd /c/MinGW/msys/1.0/lib
ln -s glfw3.dll libglfw3.dll
ln -s glfw3dll.a libglfw3dll.a

Now we have our dependencies compiled, we should be able to import the Go libraries we need, which will end up using MinGW’s compilers to interface with the C libraries.

Compiling the Go Code

In the MinGW shell used, with the PATH variable set to include MinGW/bin, set some more variables by running the following:

export GOPATH=/c/go
export CGO_CFLAGS=-I/c/MinGW/msys/1.0/include
export CGO_LDFLAGS=-L/c/MinGW/msys/1.0/lib

This allows the go tool to find out where it’s installed and also tells the cgo tool where to find the include files and libraries for the dependencies.

Install the required Go libraries by running these commands:

go get github.com/go-gl/gl
go get github.com/go-gl/glfw3

Those should complete with no errors though you will probably see some warnings when getting the go-gl/gl package.

At this point, you can take the sample code from my previous article or download it directly: goTest.go. Save the file somewhere and built it by cd’ing to that directory in the shell and running:

go build glTest.go
cp /c/MinGW/msys/1.0/lib/glfw3.dll .
./glTest.exe

If you have any problems with this, tweet me @tbogdala.

Edit 141230: Added CMake as a dependency.