How to setup OpenGL on MinGW-w64 in Windows 10 (64 bits)
I wasted an entire weekend trying to visualize a white triangle using OpenGL— equivalent to our “Hello World” programs — so I am writing down the steps which helped me slay down this beast. Cheers!
What is MinGW?
MinGW is a minimal Windows port of the GNU compiler tools, such as GCC, G++, Make, and so on. By default, code compiled in MinGW’s GCC will compile to a native Windows target, including .exe and .dll files. It’s essentially an alternative to the Microsoft Visual C++ compiler and its associated linking/make tools. Here’s a good read.
Step 1: Check if OpenGL is installed
Check if opengl32.dll and glu32.dll are present at C:\Windows\System32\. It should mostly come with the system at this location.
You can also install the OpenGL extension viewer from here which will show you the current OpenGL version installed on your system. You can update the drivers with the help of this tool. Go here for more help.
Step 2: Download MinGW-w64
> You can download the latest version of MinGW-w64 from here and extract it using 7-Zip (Download 7-Zip). I downloaded the following file: MinGW-W64 GCC-8.1.0 x86_64-posix-sjlj as the online installer didn’t work.
> After extracting, copy the mingw64 folder to C:\
> Add C:\mingw64\bin to the Path of the System Environment Variables. Guide: How to add to the Path on Windows 10.
>Also, Add C:\mingw64\x86_64-w64-mingw32\bin to the Path of the System Environment Variables.
Quick Check:
Open Command Prompt and execute ‘g++ --version
’ to see the version.
Step 3: Install CMake
CMake is a cross-platform family of tools designed to build, test, and package software. It will be used to generate MinGW makefiles required to build and install the following libraries: Freeglut and GLEW.
You can download and install CMake from here.
Note: Check the option to add CMake to the system Path. If you didn’t then find out where it’s installed and add it’s bin folder to the system Path. For instance, in my case, it was C:\Program Files\CMake\bin
Quick Check:
Open Command Prompt and execute ‘cmake --version
’ to see the version.
Step 4: Install FreeGLUT
FreeGLUT is an open-source alternative to GLUT (OpenGL Utility Toolkit) library which allows the user to create and manage windows containing OpenGL contexts on a wide range of platforms and also read the mouse, keyboard and joystick functions. Source: Wikipedia.
> Download the latest Freeglut from here. I downloaded Freeglut 3.0.0. It must be a tar.gz file so you might have to extract it twice using 7-Zip.
> Go to the freeglut folder (it should contain CMakeLists.txt file) and open the Command Prompt at this location to execute the following command:
Use “cmake --help
” to learn about the different options (-G, -S, -B, -D)
CMAKE_INSTALL_PREFIX is set to the location where we want to install the Freeglut library files so that our future OpenGL code can use its headers.
> Next execute: “mingw32-make all
”
mingw32-make is part of the C:\mingw64\bin suite
Note: You might get a lot of warnings but don’t panic.
> Finally execute: “mingw32-make install
” so that the include headers, lib and bin files are copied to the corresponding folders of CMAKE_INSTALL_PREFIX (which is C:\mingw64\x86_64-w64-mingw32)
Step 5: Install GLEW
The OpenGL Extension Wrangler Library (GLEW) is a cross-platform C/C++ library that helps in querying and loading OpenGL extensions. GLEW provides efficient run-time mechanisms for determining which OpenGL extensions are supported on the target platform. Source: Wikipedia.
The steps to install GLEW are similar to Step 4 (Installing FreeGLUT)
> Download the latest GLEW from here. I downloaded GLEW 2.1.0. Extract the compressed file.
> Go the glew folder and search for the location of CMakeLists.txt
It’s here: glew-2.1.0\build\cmake\CMakeLists.txt
Open the Command Prompt at this location and execute the following command:
> Next execute: “mingw32-make all
”
> Finally execute: “mingw32-make install
”
Step 6: Write Example OpenGL Code
Its time to reap the benefits of the previous steps. Create a simple OpenGL example in C++ as follows and call the file triangle.cpp
It’s definitely a lot of effort to render a simple white triangle but later you can reuse the code and it teaches you the essential parts of OpenGL programming.
Run the command: g++ triangle.cpp -o triangle -lopengl32 -lglew32 -lfreeglut -lglu32
It will create the executable triangle.exe which you can run from the Command Prompt or open it from its folder as any application exe.
Additional Info & Debug
> GLFW is a lightweight utility library for use with OpenGL. Its a game-centric alternative to FreeGLUT. You can compile and install GLFW similar to the way we installed FreeGLUT and GLEW. You can download the source from here. Useful links: FreeGLUT vs glfw Wikipedia
> If you get errors such as “No such file or directory” for an imported Header file, it means that header file is not in the search paths of g++/gcc. You can check where the g++ tool is searching for header files by running with “-v
” option as follows:g++ -v triangle.cpp -o triangle -lopengl32 -lglew32 -lfreeglut -lglu32
Make sure the headers are available in those search paths by copying them or reinstalling by changing the install location (CMAKE_INSTALL_PREFIX flag)
Similarly, if you face .dll not found errors then make sure the dll’s location is added to the Path of the System Environment Variables.
Step 7: Install GLFW (Optional)
> Download the latest GLFW source package from here. I downloaded glfw-3.2.1 and extracted it.
> Open Command Prompt, go to its folder and execute the following:
> Next execute: “mingw32-make all
”
> Finally execute: “mingw32-make install
”
Finale
Thanks for reading my guide and hope it helped you set up the OpenGL environment. Feel free to comment with questions and feedback. Cheers! :)